From 0ec7b3ea888f82e1977841891e9a7fac22b79a44 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Fri, 28 Aug 2026 14:38:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=A8=A1=E5=BC=8F=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=20=20=E5=8C=BA=E5=9F=9F=E8=BF=98=E6=98=AF=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Map.jsx | 160 ++++++++++++++++++++++- src/components/devices/RobotTaskPage.tsx | 46 ++++++- 2 files changed, 202 insertions(+), 4 deletions(-) diff --git a/src/components/Map.jsx b/src/components/Map.jsx index c6a734b..33ad77f 100644 --- a/src/components/Map.jsx +++ b/src/components/Map.jsx @@ -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; diff --git a/src/components/devices/RobotTaskPage.tsx b/src/components/devices/RobotTaskPage.tsx index 31100ba..556ec6e 100644 --- a/src/components/devices/RobotTaskPage.tsx +++ b/src/components/devices/RobotTaskPage.tsx @@ -66,6 +66,8 @@ export default function RobotTaskPage() { const [taskList, setTaskList] = useState([]); const [arrivedPoint, setArrivedPoint] = useState(null);//完成点 const { modal } = App.useApp(); + const [polyPoints, setPolyPoints] = useState([]); + @@ -77,6 +79,7 @@ export default function RobotTaskPage() { const [focusLocation, setFocusLocation] = useState(null); const [points, setPoints] = useState([]); + const [routeMode, setRouteMode] = useState(''); const [clearNavLine, setClearNavLine] = useState(0); const [clearPlanLine, setClearPlanLine] = useState(0); @@ -410,6 +413,43 @@ export default function RobotTaskPage() { } }, [taskPoolList]); + const getRouteOut = async () => { + try { + + const res = await getSingleRouteList({ id: selectedPathWork?.id }); + // coverage(区域覆盖)绘制为多边形,其余为折线/单点 + const mode = res?.data?.mode; + setRouteMode(mode); + + if (res?.code === 200 && res?.data?.jsonData) { + let pathPoints = []; + + try { + // 尝试解析 JSON + pathPoints = res?.data?.jsonData?.path || []; + } catch (err) { + console.error('解析路线数据失败:', err); + } + + if (pathPoints.length === 0) { + utils.message.warning(t('devices.route2')); + } + + setPolyPoints(pathPoints); + + } else { + setPolyPoints([]); + setRouteMode(''); + } + } catch (err) { + console.error('获取路线详情失败:', err); + setPolyPoints([]); + setRouteMode(''); + } + } + useEffect(() => { + getRouteOut(); + }, [selectedPathWork?.id]) // 调用接口获取路线详情 const fetchRouteDetail = async (id) => { console.log(id, "获取正在执行任务的id") @@ -420,6 +460,7 @@ export default function RobotTaskPage() { const res = await getTaskPath(params) //const res = await getSingleRouteList({ id: selectedPathWork.id }); + if (res?.code === 200 && res?.data) { let pathPoints = []; @@ -1161,7 +1202,7 @@ export default function RobotTaskPage() { extra={ {/* Space开启自动换行,小屏按钮自动折行 */} - + {/* @@ -1179,12 +1220,14 @@ export default function RobotTaskPage() { points={points} focusLocation={focusLocation} realtimePosition={realtimePosition} + polyPoints={polyPoints} realtimeType="robot" from="deviceStatus" drawRealTime={execute} finishPoint={arrivedPoint} ifClearNavLine={clearNavLine} ifClearPlanLine={clearPlanLine} + routeMode={routeMode} /> {/* 机器人状态悬浮面板 - 绝对定位左上角 */} @@ -1840,6 +1883,7 @@ const NewTaskModal = ({ isOpen, onClose, robots, selectedPathWork, handlGetTaskL if (selectedPathWork) { setFormData(prev => ({ ...prev, path: selectedPathWork })); } + }, [selectedPathWork]); // ====================== 周/月选择 ======================