f封装完成点 无人机和 机器人 地图清空实时轨迹和清空规划轨迹

This commit is contained in:
mmc
2026-06-11 17:02:23 +08:00
parent c9bc863354
commit 26dadc95cf
6 changed files with 586 additions and 265 deletions

View File

@@ -4,10 +4,11 @@ import inspectImg from "../assets/images/inspectCar.png"
import planeImg from '../assets/images/plane.png';
import cameraImg from '../assets/images/camera.png';
import droneInspectImg from '../assets/images/xunjiancar.png';
import gecaocar from '../assets/images/gecaocar.png';
import gecocar from '../assets/images/gecaocar.png';
import arrowImg from '../assets/images/arrow.png';
import iotImg from '../assets/images/iot.png';
import envConfig from '../../env';
import { TracePoint, TPAction, TPMode } from '../lib/TracePoint/tracePoint';
const CYAN_ALARM_COLOR = new window.Cesium.Color(
@@ -25,7 +26,7 @@ const CYAN_GLOW_COLOR = new window.Cesium.Color(
);
window.Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmNjk0NjI1MC0wZGYyLTQ1YzItYjJmZi0wY2M5ODEwODllYzIiLCJpZCI6Mzk3MTQxLCJpYXQiOjE3NzI1MjkwMjV9.U6qmakhDgXJVXwLFyWzSxAUOFZRrDkIsRUPP6CCdnKg";
export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], onPathChange, from, onClearDraw }) {
export default function CesiumMap({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], onPathChange, from, onClearDraw }) {
const containerRef = useRef(null);
const viewerRef = useRef(null);
const tooltipRef = useRef(null);
@@ -34,12 +35,18 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
const entityMapRef = useRef(new Map());
const alarmMapRef = useRef(new Map());
const iotMapRef = useRef(new Map());
const trackLineRef = useRef(null);
const planLineRef = useRef(null); // points 规划航线
const navLineRef = useRef(null); // 完成点实时轨迹
const realtimeEntityRef = useRef(null);
const realtimeLineRef = useRef(null);
const realtimeTrackRef = useRef([]);
const tracePointRef = useRef(new TracePoint());
const lastFinishPointRef = useRef(null);
const isNavigationModeRef = useRef(false);
const followCarEntityRef = useRef(null); // 跟随小车的实例
const isCameraFlyingRef = useRef(false);
const lastFlightPromiseRef = useRef(null);
@@ -171,8 +178,8 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
!isNaN(Number(realtimePosition.lat))
);
if (trackLineRef.current) {
viewer.entities.remove(trackLineRef.current);
if (planLineRef.current) {
viewer.entities.remove(planLineRef.current);
}
// 2. 传入的 points 本身为空 → 清空轨迹退出
@@ -180,9 +187,9 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
const viewer = viewerRef.current;
if (viewer) {
try {
if (trackLineRef.current) {
viewer.entities.remove(trackLineRef.current);
trackLineRef.current = null;
if (planLineRef.current) {
viewer.entities.remove(planLineRef.current);
planLineRef.current = null;
}
} catch (e) { }
viewer.scene.requestRender(); // 强制刷新!
@@ -216,14 +223,14 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
polyline: {
positions: positions,
width: 2,
// 实线 + 纯蓝色(核心修改)
material: Cesium.Color.BLUE.withAlpha(0.9), // 半透明蓝色实线,更美观
// 规划航线用橙色,和实时轨迹区分
material: Cesium.Color.ORANGE.withAlpha(0.8),
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
});
trackLineRef.current = trackLine;
planLineRef.current = trackLine;
// ======================
// 🚀 非实时模式 -> 自动飞到轨迹
@@ -821,6 +828,203 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
}
const yellowCircleImg = useMemo(() => createYellowCircleDataUrl(30), []);
// 处理完成点
useEffect(() => {
if (!finishPoint || !finishPoint.lng || !finishPoint.lat || !drawRealTime) {
// 没有 finishPoint 时,移除跟随小车
const viewer = viewerRef.current;
if (viewer && followCarEntityRef.current) {
viewer.entities.remove(followCarEntityRef.current);
followCarEntityRef.current = null;
}
return;
}
console.log(22222222222)
const point = { lng: finishPoint.lng, lat: finishPoint.lat };
console.log(isNavigationModeRef.current, drawRealTime, 8888888888)
// 如果是定位模式且还没有导航模式,则切换到导航模式
if (!isNavigationModeRef.current && drawRealTime) {
tracePointRef.current.setMode(TPMode.NAVIGATION);
isNavigationModeRef.current = true;
}
console.log(444444444)
tracePointRef.current.upsert(point, TPAction.ADD);
lastFinishPointRef.current = finishPoint;
// 更新轨迹绘制
updateTraceLine();
// 更新或添加跟随小车
}, [finishPoint, drawRealTime]);
// 更新跟随小车
const updateFollowCar = (cartesianPos, heading = 0) => {
const viewer = viewerRef.current;
if (!viewer || !cartesianPos) return;
const Cesium = window.Cesium;
const hpr = new Cesium.HeadingPitchRoll(heading, 0, 0);
const orientation = Cesium.Transforms.headingPitchRollQuaternion(cartesianPos, hpr);
if (followCarEntityRef.current) {
followCarEntityRef.current.position = cartesianPos;
followCarEntityRef.current.orientation = orientation;
} else {
followCarEntityRef.current = viewer.entities.add({
position: cartesianPos,
orientation: orientation,
billboard: {
image: carImg,
width: 50,
height: 50,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
color: Cesium.Color.YELLOW.withAlpha(0.8),
},
});
}
};
const updateTraceLine = (clear = false) => {
const viewer = viewerRef.current;
if (!viewer) return;
const Cesium = window.Cesium;
// 主动清空 或 轨迹数据为空:统一清理
if (clear) {
tracePointRef.current.reset?.();
lastFinishPointRef.current = null;
isNavigationModeRef.current = false;
realtimeCartesianRef.current = [];
realtimeLastPointRef.current = null;
if (navLineRef.current) {
viewer.entities.remove(navLineRef.current);
navLineRef.current = null;
}
if (followCarEntityRef.current) {
viewer.entities.remove(followCarEntityRef.current);
followCarEntityRef.current = null;
}
if (realtimeLineEntityRef.current) {
viewer.entities.remove(realtimeLineEntityRef.current);
realtimeLineEntityRef.current = null;
}
viewer.scene.requestRender();
return;
}
const tracePoints = tracePointRef.current.getTracePoint();
if (!tracePoints || tracePoints.length === 0) {
if (navLineRef.current) {
viewer.entities.remove(navLineRef.current);
navLineRef.current = null;
}
if (followCarEntityRef.current) {
viewer.entities.remove(followCarEntityRef.current);
followCarEntityRef.current = null;
}
viewer.scene.requestRender();
return;
}
console.log(tracePoints, tracePointRef.current.getMode(), "mode1111111111");
const validPoints = tracePoints.filter(p => {
const lng = Number(p.lng);
const lat = Number(p.lat);
return !isNaN(lng) && !isNaN(lat) && Math.abs(lng) < 180 && Math.abs(lat) < 90 && lng !== 0 && lat !== 0;
});
if (validPoints.length === 0) {
if (followCarEntityRef.current) {
viewer.entities.remove(followCarEntityRef.current);
followCarEntityRef.current = null;
}
return;
}
const positions = validPoints.map(point =>
Cesium.Cartesian3.fromDegrees(Number(point.lng), Number(point.lat))
);
// 更新轨迹线
if (navLineRef.current) {
navLineRef.current.polyline.positions = positions;
} else {
navLineRef.current = viewer.entities.add({
polyline: {
positions: positions,
width: 2,
material: Cesium.Color.BLUE.withAlpha(0.9),
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
});
}
// 计算小车朝向
let heading = 0;
const lastIdx = positions.length - 1;
if (lastIdx > 0) {
const prevCart = positions[lastIdx - 1];
const currCart = positions[lastIdx];
const prevCartographic = Cesium.Cartographic.fromCartesian(prevCart);
const currCartographic = Cesium.Cartographic.fromCartesian(currCart);
const dLon = currCartographic.longitude - prevCartographic.longitude;
const dLat = currCartographic.latitude - prevCartographic.latitude;
heading = Math.atan2(dLat, dLon);
}
// 直接传 Cartesian3,无需二次转换
//updateFollowCar(positions[lastIdx], heading);
viewer.scene.requestRender();
};
// 清空规划航线
useEffect(() => {
if (ifClearPlanLine) {
const viewer = viewerRef.current;
if (viewer) {
if (planLineRef.current) {
viewer.entities.remove(planLineRef.current);
planLineRef.current = null;
}
viewer.scene.requestRender();
}
}
}, [ifClearPlanLine]);
// 清空实时轨迹
useEffect(() => {
if (ifClearNavLine) {
updateTraceLine(true);
}
}, [ifClearNavLine]);
// 兼容旧 ifClear:同时清空两类
useEffect(() => {
if (ifClear) {
const viewer = viewerRef.current;
if (viewer) {
if (planLineRef.current) {
viewer.entities.remove(planLineRef.current);
planLineRef.current = null;
}
updateTraceLine(true);
}
}
}, [ifClear])
useEffect(() => {
const viewer = viewerRef.current;
@@ -1173,6 +1377,11 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
viewer.scene.requestRender();
// =============================
// 判断是否是无人机模式
// =============================
const isDroneMode = realtimeType === 'plane' || realtimeType === 'drone' || devices.some(d => d.type === 'plane' || d.type === 'drone');
// =============================
// 判断实时数据是否合法
// =============================
@@ -1195,6 +1404,7 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
realtimeCarEntityRef.current = null;
}
// 移除实时轨迹线
if (realtimeLineEntityRef.current) {
viewer.entities.remove(realtimeLineEntityRef.current);
realtimeLineEntityRef.current = null;
@@ -1222,15 +1432,6 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
return;
}
if (!drawRealTime) {
if (realtimeLineEntityRef.current) {
viewer.entities.remove(realtimeLineEntityRef.current);
realtimeLineEntityRef.current = null;
}
// 轨迹点数组也清空
realtimeCartesianRef.current = [];
}
// ====================================
// 情况2:有实时数据 -> 隐藏 focusEntity
// ====================================
@@ -1258,48 +1459,147 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
const position = Cesium.Cartesian3.fromDegrees(lon, lat, 1);
// ====================================
// 轨迹点去抖动(防止重复点)
// 无人机模式:保持原来的逻辑
// ====================================
if (isDroneMode) {
// 轨迹点去抖动(防止重复点)
const last = realtimeLastPointRef.current;
let shouldAdd = true;
const last = realtimeLastPointRef.current;
let shouldAdd = true;
if (last) {
const dx = last.lon - lon;
const dy = last.lat - lat;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 0.0000001) {
shouldAdd = false;
if (last) {
const dx = last.lon - lon;
const dy = last.lat - lat;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 0.0000001) {
shouldAdd = false;
}
}
}
if (shouldAdd && drawRealTime) {
realtimeLastPointRef.current = { lon, lat };
realtimeCartesianRef.current.push(
Cesium.Cartesian3.fromDegrees(lon, lat)
);
}
if (shouldAdd) {
// 第一次进入 realtime -> 隐藏所有设备
if (!realtimeHiddenRef.current) {
realtimeHiddenRef.current = true;
realtimeLastPointRef.current = { lon, lat };
entityMapRef.current.forEach(e => {
if (e.billboard) e.billboard.show = false;
});
realtimeCartesianRef.current.push(
Cesium.Cartesian3.fromDegrees(lon, lat)
);
alarmMapRef.current.forEach(e => {
if (e.billboard) e.billboard.show = false;
});
iotMapRef.current.forEach(e => {
if (e.billboard) e.billboard.show = false;
});
}
// 无人机图标
const img = planeImg;
const imgWidth = 70;
const imgHeight = 70;
// 如果有航向角,设置旋转
let rotation = 0;
if (realtimePosition?.heading !== undefined && !isNaN(Number(realtimePosition.heading))) {
let headingDeg = Number(realtimePosition.heading);
rotation = Cesium.Math.toRadians(headingDeg);
}
if (!realtimeCarEntityRef.current) {
realtimeCarEntityRef.current = viewer.entities.add({
position: position,
billboard: {
image: img,
width: imgWidth,
height: imgHeight,
verticalOrigin: Cesium.VerticalOrigin.CENTER,
rotation: rotation,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
}
});
} else {
realtimeCarEntityRef.current.position = position;
realtimeCarEntityRef.current.billboard.image = img;
realtimeCarEntityRef.current.billboard.width = imgWidth;
realtimeCarEntityRef.current.billboard.height = imgHeight;
realtimeCarEntityRef.current.billboard.rotation = rotation;
}
// 无人机模式的实时轨迹线
if (drawRealTime) {
if (realtimeLineEntityRef.current) {
// 已存在,CallbackProperty 会自动更新
} else {
const callbackPositions = new Cesium.CallbackProperty(() => {
return realtimeCartesianRef.current;
}, false);
realtimeLineEntityRef.current = viewer.entities.add({
polyline: {
positions: callbackPositions,
width: 5,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.25,
color: Cesium?.Color.LIME
}),
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
}
} else {
// 不绘制实时轨迹,移除线
if (realtimeLineEntityRef.current) {
viewer.entities.remove(realtimeLineEntityRef.current);
realtimeLineEntityRef.current = null;
}
realtimeCartesianRef.current = [];
}
// 无人机模式不需要完成点逻辑,直接返回
if (!hasFlownRef.current) {
hasFlownRef.current = true;
if (getCameraMode() !== "realtime") return;
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(lon, lat, 30),
orientation: {
heading: 0,
pitch: Cesium.Math.toRadians(-90),
roll: 0
},
duration: 1.2
});
}
viewer.scene.requestRender();
return;
}
// ====================================
// 限制轨迹长度(防止内存爆)
// 机器人模式:使用 TracePoint 逻辑
// ====================================
const point = { lng: lon, lat: lat };
//if (realtimeCartesianRef.current.length > 2000) {
// realtimeCartesianRef.current.shift();
//}
// 如果是定位模式且 drawRealTime 为 false,保持定位模式
if (!drawRealTime) {
tracePointRef.current.setMode(TPMode.LOCATION);
isNavigationModeRef.current = false;
}
// 更新当前点
tracePointRef.current.upsert(point, TPAction.UPDATE);
// 更新轨迹绘制
updateTraceLine();
// ====================================
// 第一次进入 realtime -> 隐藏所有设备
// ====================================
if (!realtimeHiddenRef.current) {
realtimeHiddenRef.current = true;
@@ -1318,23 +1618,14 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
}
// ====================================
// 创建实时设备图标(只创建一次)
// ====================================
// 判断应该用什么图标(飞机优先)
const isPlane = realtimeType === 'plane' || realtimeType === 'drone' || devices.some(d => d.type === 'plane' || d.type === 'drone');
const img = isPlane ? planeImg : carImg;
const imgWidth = isPlane ? 70 : 60;
const imgHeight = isPlane ? 70 : 60;
// 机器人图标
const img = carImg;
const imgWidth = 60;
const imgHeight = 60;
// 如果有航向角,设置旋转
let rotation = 0;
if (realtimePosition?.heading !== undefined && !isNaN(Number(realtimePosition.heading))) {
// 航向通常是地理坐标系(0=北, 90=东, 顺时针)
// Cesium billboard 的 rotation 是0=右(东),顺时针为正
// 需要将地理航向转换为 Cesium rotation
// 地理 0° 北 → Cesium 需要 -90° 或者 270°(因为 Cesium 0° 是右/东)
let headingDeg = Number(realtimePosition.heading);
rotation = Cesium.Math.toRadians(headingDeg);
}
@@ -1352,7 +1643,6 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
}
});
} else {
// 更新位置、图标和旋转
realtimeCarEntityRef.current.position = position;
realtimeCarEntityRef.current.billboard.image = img;
realtimeCarEntityRef.current.billboard.width = imgWidth;
@@ -1360,42 +1650,57 @@ export default function CesiumMap({ drawRealTime, realtimePosition, realtimeType
realtimeCarEntityRef.current.billboard.rotation = rotation;
}
// ====================================
// 创建轨迹线(只创建一次)
// ====================================
// 机器人模式只在导航模式下显示额外的实时轨迹线
if (drawRealTime && isNavigationModeRef.current) {
// 轨迹点去抖动(防止重复点)
const last = realtimeLastPointRef.current;
let shouldAdd = true;
if (!realtimeLineEntityRef.current) {
if (last) {
const dx = last.lon - lon;
const dy = last.lat - lat;
const dist = Math.sqrt(dx * dx + dy * dy);
//if (dist < 0.0000001) {
// shouldAdd = false;
//}
}
const callbackPositions = new Cesium.CallbackProperty(() => {
return realtimeCartesianRef.current;
}, false);
if (!drawRealTime) return;
//if (shouldAdd) {
// realtimeLastPointRef.current = { lon, lat };
// realtimeCartesianRef.current.push(
// Cesium.Cartesian3.fromDegrees(lon, lat)
// );
//}
realtimeLineEntityRef.current = viewer.entities.add({
polyline: {
positions: callbackPositions,
width: 5,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.25,
color: Cesium?.Color.LIME
}),
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY
}
});
//if (realtimeLineEntityRef.current) {
// // 已存在,CallbackProperty 会自动更新
//} else {
// const callbackPositions = new Cesium.CallbackProperty(() => {
// return realtimeCartesianRef.current;
// }, false);
// realtimeLineEntityRef.current = viewer.entities.add({
// polyline: {
// positions: callbackPositions,
// width: 5,
// material: new Cesium.PolylineGlowMaterialProperty({
// glowPower: 0.25,
// color: Cesium?.Color.LIME
// }),
// clampToGround: true,
// disableDepthTestDistance: Number.POSITIVE_INFINITY
// }
// });
//}
} else {
// 移除实时轨迹线
if (realtimeLineEntityRef.current) {
viewer.entities.remove(realtimeLineEntityRef.current);
realtimeLineEntityRef.current = null;
}
realtimeCartesianRef.current = [];
}
if (!hasFlownRef.current) {
hasFlownRef.current = true;