根据模式显示 区域还是线

This commit is contained in:
mmc
2026-08-28 14:38:45 +08:00
parent 2263b190f5
commit 0ec7b3ea88
2 changed files with 202 additions and 4 deletions

View File

@@ -27,7 +27,7 @@ const CYAN_GLOW_COLOR = new window.Cesium.Color(
);
window.Cesium.Ion.defaultAccessToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmNjk0NjI1MC0wZGYyLTQ1YzItYjJmZi0wY2M5ODEwODllYzIiLCJpZCI6Mzk3MTQxLCJpYXQiOjE3NzI1MjkwMjV9.U6qmakhDgXJVXwLFyWzSxAUOFZRrDkIsRUPP6CCdnKg";
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint }, ref) => {
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint, routeMode = '', polyPoints }, ref) => {
const containerRef = useRef(null);
const viewerRef = useRef(null);
const tooltipRef = useRef(null);
@@ -37,6 +37,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
const alarmMapRef = useRef(new Map());
const iotMapRef = useRef(new Map());
const planLineRef = useRef(null); // points 规划航线
const polygonRef = useRef(null); // coverage 区域多边形
const navLineRef = useRef(null); // 完成点实时轨迹
const markedPointsRef = useRef([]); // 标记点实体
const { t } = useTranslation();
@@ -203,7 +204,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}
return;
}
const validPoints = points.filter(p => {
const validPoints = points?.filter(p => {
const lon = Number(p.lng);
const lat = Number(p.lat);
@@ -296,6 +297,159 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
};
const drawPolygon = (points) => {
const viewer = viewerRef.current;
if (!viewer) return;
const isRealtimeMode = !!(
realtimePosition?.lon &&
realtimePosition?.lat &&
!isNaN(Number(realtimePosition.lon)) &&
!isNaN(Number(realtimePosition.lat))
);
const Cesium = window.Cesium;
// 每次绘制区域前,先移除上一次绘制的地块
if (polygonRef.current) {
try {
viewer.entities.remove(polygonRef.current);
polygonRef.current = null;
} catch (e) { }
}
if (planLineRef.current) {
try {
viewer.entities.remove(planLineRef.current);
planLineRef.current = null;
} catch (e) { }
}
const validPoints = points?.filter(p => {
const lon = Number(p.lng);
const lat = Number(p.lat);
return (
!isNaN(lon) &&
!isNaN(lat) &&
Math.abs(lon) < 180 &&
Math.abs(lat) < 90 &&
lon !== 0 &&
lat !== 0
);
});
if (validPoints.length === 0) return;
const positions = validPoints.map(point =>
Cesium.Cartesian3.fromDegrees(
Number(point.lng),
Number(point.lat)
)
);
// coverage(区域覆盖)模式下以多边形 polygon 框绘制,否则为折线
const isCoverage = routeMode === 'coverage';
let trackLine;
if (isCoverage) {
trackLine = viewer.entities.add({
polygon: {
hierarchy: positions,
material: Cesium.Color.ORANGE.withAlpha(0.2),
outline: true,
outlineColor: Cesium.Color.ORANGE.withAlpha(0.8),
height: 0,
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
});
} else {
trackLine = viewer.entities.add({
polyline: {
positions: positions,
width: 2,
material: Cesium.Color.ORANGE.withAlpha(0.8),
clampToGround: true,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
});
}
polygonRef.current = trackLine;
// ======================
// 🚀 非实时模式 -> 自动飞到轨迹
// ======================
if (!isRealtimeMode) {
let minLon = 180, maxLon = -180;
let minLat = 90, maxLat = -90;
validPoints.forEach(p => {
minLon = Math.min(minLon, p.lng);
maxLon = Math.max(maxLon, p.lng);
minLat = Math.min(minLat, p.lat);
maxLat = Math.max(maxLat, p.lat);
});
if (getCameraMode() !== "track") return;
// 单个点
if (validPoints.length === 1) {
const p = validPoints[0];
viewer.scene.requestRender();
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(
p.lng,
p.lat,
800
),
duration: 1.2
});
viewer.scene.requestRender();
} else {
const rectangle = Cesium.Rectangle.fromDegrees(
minLon,
minLat,
maxLon,
maxLat
);
viewer.scene.requestRender();
viewer.camera.flyTo({
destination: rectangle,
duration: 1.2
});
viewer.scene.requestRender();
}
}
};
useEffect(() => {
if (polyPoints && polyPoints.length > 0) {
drawPolygon(polyPoints)
} else {
// polyPoints 被清空时,移除已绘制的区域
const viewer = viewerRef.current;
if (viewer && polygonRef.current) {
try {
viewer.entities.remove(polygonRef.current);
polygonRef.current = null;
} catch (e) { }
}
}
}, [polyPoints])
useEffect(() => {
if (viewerRef.current) {
@@ -1032,7 +1186,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
return;
}
const validPoints = tracePoints.filter(p => {
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;