tsg-web/src/views/Home/MapCtrl/index.js

137 lines
4.5 KiB
JavaScript
Raw Normal View History

2024-09-20 15:02:50 +08:00
import React, { useEffect, useReducer, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import FeaturePops from './Pops';
import FeatureTip from './FeatureTip';
import './index.less';
import Map2D from './M2D/Map2D';
2025-08-07 11:28:32 +08:00
import Map3D from './Map3D/Map3D';
2024-09-20 15:02:50 +08:00
import Markers from './Markers';
import { useLocation, useNavigate } from "react-router";
2025-10-27 09:16:28 +08:00
// const { Cesium } = window;
2024-09-20 15:02:50 +08:00
let id_factory_index = 1;
2025-09-10 17:13:40 +08:00
export default function MapCtrl({mode}) {
2024-09-20 15:02:50 +08:00
const location = useLocation();
const pathname = location.pathname;
const ctrlIdRef = useRef(`cloudowr_mapdiv_${id_factory_index++}`);
const mapobjRef = useRef();
const [_, forceRender] = useReducer(s => s + 1, 1);
const cameraTarget = useSelector(s => s.runtime.cameraTarget);
const layerVisible = useSelector(s => s.map.layerVisible);
2025-10-27 09:16:28 +08:00
// const map = useSelector(s => s.map.map);
2024-09-20 15:02:50 +08:00
const layerSetting = useSelector(s => s.map.layerSetting);
const clickLoopBtn = useSelector(s => s.map.clickLoopBtn);
const layerSetting1 = useSelector(s => s.runtime.layerSetting);
const layerSettingLoop = useSelector(s => s.runtime.layerSettingLoop);
const smallSkRiskTime = useSelector(s => s.runtime.smallSkRiskTime);
2025-08-07 11:28:32 +08:00
const dispatch = useDispatch();
2024-09-20 15:02:50 +08:00
useEffect(() => {
const mapCtrl = mapobjRef.current;
if (!mapCtrl) {
return;
}
mapCtrl.layerMgr.setSetting(layerSetting || {});
mapCtrl.layerMgr.setVisible(layerVisible || {});
}, [mode,layerVisible, layerSetting, mapobjRef.current]);
2024-09-20 15:02:50 +08:00
useEffect(() => {
const mapCtrl = mapobjRef.current;
if (!mapCtrl) {
return;
}
mapCtrl.zoomTo(cameraTarget);
}, [cameraTarget])
const [mapObj, setMapObj] = useState(null);
useEffect(() => {
const params = {
divid: ctrlIdRef.current,
dispatch
};
2025-08-07 11:28:32 +08:00
// const mapobj = new Map2D(params);
const mapobj = mode === '3d' ? new Map3D(params) : new Map2D(params);
2024-09-20 15:02:50 +08:00
setMapObj(mapobj);
mapobj.init();
mapobj.layerMgr.addAppLayers(dispatch, layerVisible, layerSetting, smallSkRiskTime);
mapobjRef.current = mapobj;
forceRender();
return () => {
mapobj.destroy();
mapobjRef.current = null;
}
2025-09-10 17:13:40 +08:00
}, [mode,pathname]);
2024-09-20 15:02:50 +08:00
useEffect(() => {
if (layerVisible["ContourLayer"]) {
mapObj.layerMgr.addAppLayersC(dispatch, layerVisible, layerSetting1);
}
}, [layerSetting1]);
//smallSkRiskTime
useEffect(() => {
const mapCtrl = mapobjRef.current;
if (smallSkRiskTime && smallSkRiskTime.stm && smallSkRiskTime.etm) {
mapCtrl.layerMgr.addAppLayers(dispatch, layerVisible, layerSetting, smallSkRiskTime);
}
}, [smallSkRiskTime]);
useEffect(() => {
const mapCtrl = mapobjRef.current;
if (mapCtrl) {
if (clickLoopBtn) {
mapCtrl.layerMgr.addAppLayersCLoop(dispatch, layerVisible, layerSettingLoop);
}
}
}, [layerSettingLoop, clickLoopBtn]);
2025-10-27 09:16:28 +08:00
// const myTest = (flag)=>{
// const height = flag?1:-1
// if(map){
// const primitives = map.scene.primitives;
// let waterPrimitive = null
// // //判断是否存在Primitive
// for (let i = primitives.length - 1; i >= 0; i--) {
// if (primitives.get(i).id ===('Water1')) {
// waterPrimitive = primitives.get(i)
// }
// }
// var currentModelMatrix = Cesium.Matrix4.clone(waterPrimitive.modelMatrix);
// // 定义平移向量例如沿x轴平移100单位
// var translation = new Cesium.Cartesian3(-height*0.7, height, height*0.8);
// // 创建一个平移矩阵
// var translationMatrix = Cesium.Matrix4.fromTranslation(translation);
// // 计算新的modelMatrix将平移矩阵应用到当前模型矩阵上
// var newModelMatrix = Cesium.Matrix4.multiply(translationMatrix, currentModelMatrix, new Cesium.Matrix4());
// waterPrimitive.modelMatrix = newModelMatrix;
// }
// }
2024-09-20 15:02:50 +08:00
return (
2025-09-10 17:13:40 +08:00
<div key={mode} id={ctrlIdRef.current} className="map2d3d">
2024-09-20 15:02:50 +08:00
<FeaturePops mapobj={mapobjRef.current} />
<FeatureTip />
{/*<CoordsText />*/}
<Markers mapobj={mapobjRef.current} />
2025-10-27 09:16:28 +08:00
{/* <div style={{background:'#fff',position:'absolute',display:'inline-block',left:'300px',top:"100px",zIndex:99}}>
<button onClick={()=>myTest(true)}>+</button>
<button onClick={()=>myTest(false)}>-</button>
<Input defaultValue={'2000-01-01T04:00:13Z'} onPressEnter={(e)=>{
if(map){
map.clock.currentTime = Cesium.JulianDate.fromIso8601(e.target.value);
}
}}></Input>
</div> */}
2024-09-20 15:02:50 +08:00
</div>
);
}