增加地图编辑界面,该界面支持增删改查 返航点,一个机器对应一个返航点,增删改查场站分区,将原本机器状态界面的 路径规划功能移植到该页面,地图上支持显示 返航点和分区,点击分区和返航点,地图支持定位和高亮,等待测试

机器状态界面返航 功能接口对接,还未对接实机测试
This commit is contained in:
mmc
2026-10-24 13:38:05 +08:00
parent a17a19fdc4
commit fb363882ac
10 changed files with 2137 additions and 214 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback } from 'react';
import React, { useEffect, useRef, useState, useMemo, useImperativeHandle, forwardRef, useCallback, useLayoutEffect } from 'react';
import carImg from '../assets/images/carplane.png';
import inspectImg from "../assets/images/inspectCar.png"
import planeImg from '../assets/images/planeicon.png';
@@ -10,6 +10,8 @@ import iotImg from '../assets/images/iot.png';
import envConfig from '../../env';
import { TracePoint, TPAction, TPMode } from '../lib/TracePoint/tracePoint';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { getSiteMapInfo, getStationRegions } from '../api/stationManage';
const CYAN_ALARM_COLOR = new window.Cesium.Color(
@@ -27,7 +29,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, routeMode = '', polyPoints, mapLayer }, ref) => {
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint, routeMode = '', polyPoints, mapLayer, activeType, overlayRefreshKey = 0, focusOverlay = null }, ref) => {
const containerRef = useRef(null);
const viewerRef = useRef(null);
const tooltipRef = useRef(null);
@@ -41,10 +43,20 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
const navLineRef = useRef(null); // 完成点实时轨迹
const markedPointsRef = useRef([]); // 标记点实体
const markedLineRef = useRef(null); // 标记点连线实体
const lastMarkedPointsRef = useRef([]); // 上次渲染的标记点数据,用于增量更新
const onMarkPointRef = useRef(onMarkPoint); // 总是指向最新的 onMarkPoint,避免闭包滞后
const onCameraClickRef = useRef(onCameraClick); // 同样处理,避免闭包滞后
const { t } = useTranslation();
// 场站已配置的返航点 / 分区叠加显示
const stationId = useSelector((state) => state?.station?.stationId || state?.station?.currentStation?.id);
const [stationOverlay, setStationOverlay] = useState({ returnPoints: [], regions: [] });
const stationOverlayIdsRef = useRef([]);
const returnPointPinUrlRef = useRef(null);
// 被点击聚焦的返航点 id / 图例高亮类型
const [highlightReturnPointId, setHighlightReturnPointId] = useState('');
const [legendHighlight, setLegendHighlight] = useState('');
const realtimeEntityRef = useRef(null);
const realtimeLineRef = useRef(null);
@@ -679,63 +691,94 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}
}, [points]);
// 渲染标记点 + 连线(唯一系统,避免重复标记)
// 渲染标记点 + 连线(增量更新:只增删改真正变化的点)
const drawMarkedPoints = (points) => {
const viewer = viewerRef.current;
if (!viewer) return;
// 先清空之前的标记点与连线
markedPointsRef.current.forEach(id => {
viewer.entities.removeById(id);
});
markedPointsRef.current = [];
const newPoints = points || [];
const prevPoints = lastMarkedPointsRef.current || [];
if (markedLineRef.current) {
try {
viewer.entities.remove(markedLineRef.current);
} catch (e) { }
markedLineRef.current = null;
const nextIds = new Set(newPoints.map((p, i) => `marked-point-${p.id ?? i}`));
// 1) 删除不在新列表中的旧 entity
markedPointsRef.current = markedPointsRef.current.filter(id => {
if (!nextIds.has(id)) {
viewer.entities.removeById(id);
return false;
}
return true;
});
// 2) 新增或更新标记点 entity
newPoints.forEach((p, i) => {
const id = `marked-point-${p.id ?? i}`;
const entity = viewer.entities.getById(id);
const position = window.Cesium.Cartesian3.fromDegrees(Number(p.lon), Number(p.lat));
const labelText = `${i + 1}`;
if (entity) {
// 只更新位置和 label,避免销毁重建
entity.position = position;
if (entity.label) entity.label.text = labelText;
} else {
viewer.entities.add({
id,
position,
point: {
color: window.Cesium.Color.GREEN,
pixelSize: 15,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
label: {
text: labelText,
font: '14px sans-serif',
pixelOffset: new window.Cesium.Cartesian2(0, -20),
fillColor: window.Cesium.Color.WHITE,
outlineColor: window.Cesium.Color.BLACK,
outlineWidth: 2,
style: window.Cesium.LabelStyle.FILL_AND_OUTLINE,
}
});
markedPointsRef.current.push(id);
}
});
// 3) 连线:只有坐标/数量真正变化时才重建
if (activeType !== "routeGen") {
viewer.scene.requestRender();
return;
}
const lineChanged = newPoints.length !== prevPoints.length ||
newPoints.some((p, i) => {
const pp = prevPoints[i];
return !pp || Number(pp.lon) !== Number(p.lon) || Number(pp.lat) !== Number(p.lat);
});
if (lineChanged) {
if (markedLineRef.current) {
try { viewer.entities.remove(markedLineRef.current); } catch (e) { }
markedLineRef.current = null;
}
if (newPoints.length >= 2) {
const linePositions = newPoints.map(p =>
window.Cesium.Cartesian3.fromDegrees(Number(p.lon), Number(p.lat))
);
markedLineRef.current = viewer.entities.add({
polyline: {
positions: linePositions,
width: 4,
material: window.Cesium.Color.SKYBLUE,
clampToGround: true,
}
});
}
}
if (!points || points.length === 0) return;
lastMarkedPointsRef.current = newPoints.map(p => ({ ...p }));
// 先绘制连线:有 2 个及以上打点才连线
if (points.length >= 2) {
const linePositions = points.map(p =>
window.Cesium.Cartesian3.fromDegrees(Number(p.lon), Number(p.lat))
);
markedLineRef.current = viewer.entities.add({
polyline: {
positions: linePositions,
width: 4,
material: window.Cesium.Color.SKYBLUE,
clampToGround: true,
}
});
}
points.forEach((p, i) => {
const id = `marked-point-${i}`;
viewer.entities.add({
id,
position: window.Cesium.Cartesian3.fromDegrees(p.lon, p.lat),
point: {
color: window.Cesium.Color.GREEN,
pixelSize: 15,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
label: {
text: `${i + 1}`,
font: '14px sans-serif',
pixelOffset: new window.Cesium.Cartesian2(0, -20),
fillColor: window.Cesium.Color.WHITE,
outlineColor: window.Cesium.Color.BLACK,
outlineWidth: 2,
style: window.Cesium.LabelStyle.FILL_AND_OUTLINE,
}
});
markedPointsRef.current.push(id);
});
// 强制 Cesium 立即渲染(requestRenderMode=true 时必须手动触发)
viewer.scene.requestRender();
};
useEffect(() => {
@@ -834,6 +877,25 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}
}
// 返航点悬浮提示:显示别名与该返航点绑定的机器
function showReturnPointTooltip(tooltip, position, point) {
if (!tooltip) return;
tooltip.style.display = "block";
tooltip.style.left = position.x + 12 + "px";
tooltip.style.top = position.y + 12 + "px";
tooltip.style.whiteSpace = "nowrap";
const boundDevice = devices.find(d => d?.serialNumber === point.bindDeviceId);
const deviceLabel = point.bindDeviceId
? (boundDevice?.deviceAlias || boundDevice?.deviceName || point.bindDeviceId)
: t('devices.mapEditor.unbound');
tooltip.innerHTML = `
<div style="font-weight:600;margin-bottom:4px;">📍 ${point.alias || '-'}</div>
<div>🤖 ${deviceLabel}</div>
`;
}
function fetchPublishedLayers() {
const baseUrl = envConfig.geoserver_url;
//const baseUrl = 'https://gis.caasrobot.com/geoserver';
@@ -1271,6 +1333,163 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
flyToLayer(mapLayer, envConfig.geoserver_url, 100);
}, [mapLayer]);
// 拉取当前场站已配置的返航点与分区
useEffect(() => {
if (!stationId) {
setStationOverlay({ returnPoints: [], regions: [] });
return;
}
let cancelled = false;
getSiteMapInfo({ siteId: stationId })
.then((res) => {
if (cancelled) return;
const pts = res?.code === 200 ? (res?.data?.restPoints || []) : [];
setStationOverlay(prev => ({ ...prev, returnPoints: Array.isArray(pts) ? pts : [] }));
})
.catch(() => { });
getStationRegions({ siteId: stationId })
.then((res) => {
if (cancelled) return;
const list = res?.code === 200 ? (res?.data || res?.rows || []) : [];
setStationOverlay(prev => ({ ...prev, regions: Array.isArray(list) ? list : [] }));
})
.catch(() => { });
return () => { cancelled = true; };
}, [stationId, overlayRefreshKey]);
// 在地图上叠加显示:返航点用标志图标,分区用小框
useEffect(() => {
const viewer = viewerRef.current;
const Cesium = window.Cesium;
if (!viewer || !Cesium) return;
// 清理上一次叠加实体
stationOverlayIdsRef.current.forEach(id => viewer.entities.removeById(id));
stationOverlayIdsRef.current = [];
// 返航点标志图标(PinBuilder 生成,失败则退化为圆点)
if (!returnPointPinUrlRef.current && Cesium.PinBuilder) {
try {
returnPointPinUrlRef.current = Cesium.PinBuilder.fromColor(Cesium.Color.fromCssColorString('#165DFF'), 36).toDataURL();
} catch (e) { /* ignore */ }
}
(stationOverlay.returnPoints || []).forEach((pt, idx) => {
const lon = Number(pt?.position?.lng ?? pt?.lng);
const lat = Number(pt?.position?.lat ?? pt?.lat);
if (!isFinite(lon) || !isFinite(lat) || lon === 0 || lat === 0) return;
const id = `station-return-point-${idx}`;
const isHighlighted = id === highlightReturnPointId;
viewer.entities.add({
id,
returnPointData: {
alias: pt?.alias || '',
bindDeviceId: pt?.bindDeviceId || '',
},
position: Cesium.Cartesian3.fromDegrees(lon, lat, 0),
...(returnPointPinUrlRef.current
? {
billboard: {
image: returnPointPinUrlRef.current,
width: isHighlighted ? 36 : 24,
height: isHighlighted ? 48 : 32,
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
}
}
: {
point: {
pixelSize: isHighlighted ? 16 : 10,
color: Cesium.Color.fromCssColorString(isHighlighted ? '#FA8C16' : '#165DFF'),
disableDepthTestDistance: Number.POSITIVE_INFINITY,
}
}),
label: pt?.alias ? {
text: pt.alias,
font: 'bold 12px sans-serif',
fillColor: Cesium.Color.WHITE,
showBackground: true,
backgroundColor: Cesium.Color.fromCssColorString(isHighlighted ? '#FA8C16' : '#165DFF').withAlpha(0.8),
pixelOffset: new Cesium.Cartesian2(0, isHighlighted ? -48 : -34),
disableDepthTestDistance: Number.POSITIVE_INFINITY,
} : undefined,
});
stationOverlayIdsRef.current.push(id);
});
// 分区:用小框(多边形)显示
(stationOverlay.regions || []).forEach((region, idx) => {
const scope = Array.isArray(region?.scope) ? region.scope : [];
const positions = scope
.map(p => {
const lon = Number(p?.lng ?? p?.lon);
const lat = Number(p?.lat);
if (!isFinite(lon) || !isFinite(lat) || lon === 0 || lat === 0) return null;
return Cesium.Cartesian3.fromDegrees(lon, lat, 0);
})
.filter(Boolean);
if (positions.length < 3) return;
const id = `station-region-${region?.id ?? idx}`;
viewer.entities.add({
id,
polygon: {
hierarchy: positions,
material: Cesium.Color.fromCssColorString('#13C2C2').withAlpha(0.18),
outline: true,
outlineColor: Cesium.Color.fromCssColorString('#13C2C2').withAlpha(0.9),
height: 0,
clampToGround: true,
},
label: region?.regionName ? {
text: region.regionName,
font: '12px sans-serif',
fillColor: Cesium.Color.WHITE,
showBackground: true,
backgroundColor: Cesium.Color.fromCssColorString('#13C2C2').withAlpha(0.8),
disableDepthTestDistance: Number.POSITIVE_INFINITY,
} : undefined,
});
stationOverlayIdsRef.current.push(id);
});
viewer.scene.requestRender();
}, [stationOverlay, highlightReturnPointId]);
// 点击已配置列表:定位到对应返航点/分区;返航点额外高亮
useEffect(() => {
const viewer = viewerRef.current;
const Cesium = window.Cesium;
if (!viewer || !Cesium || !focusOverlay) return;
if (focusOverlay.type === 'returnPoint') {
const pt = (stationOverlay.returnPoints || [])[focusOverlay.index];
const lon = Number(pt?.position?.lng ?? pt?.lng);
const lat = Number(pt?.position?.lat ?? pt?.lat);
if (!isFinite(lon) || !isFinite(lat) || lon === 0 || lat === 0) return;
setLegendHighlight('returnPoint');
setHighlightReturnPointId(`station-return-point-${focusOverlay.index}`);
viewer.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(lon, lat, 500),
duration: 0.8,
});
} else if (focusOverlay.type === 'region') {
const region = (stationOverlay.regions || []).find(r => String(r.id) === String(focusOverlay.id));
const scope = Array.isArray(region?.scope) ? region.scope : [];
const lons = scope.map(p => Number(p?.lng ?? p?.lon)).filter(n => isFinite(n) && n !== 0);
const lats = scope.map(p => Number(p?.lat)).filter(n => isFinite(n) && n !== 0);
if (!lons.length || !lats.length) return;
setLegendHighlight('region');
setHighlightReturnPointId('');
viewer.camera.flyTo({
destination: Cesium.Rectangle.fromDegrees(
Math.min(...lons), Math.min(...lats), Math.max(...lons), Math.max(...lats)
),
duration: 0.8,
});
}
}, [focusOverlay, stationOverlay]);
useEffect(() => {
// 如果有 realtimePosition,不显示 focusEntity
@@ -1960,10 +2179,13 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
handler.setInputAction((movement) => {
const picked = viewer.scene.pick(movement.endPosition);
const pickedReturnPoint = (window.Cesium.defined(picked) && picked.id?.returnPointData) ? picked.id.returnPointData : null;
const pickedRobot = (window.Cesium.defined(picked) && picked.id?.properties?.robot) ? picked.id.properties.robot.getValue() : null;
// ✅ 只有拾取到设备实体,才显示tooltip,空白区域直接隐藏
if (pickedRobot) {
// 返航点优先显示「别名 + 绑定机器」,设备显示设备信息,空白区域隐藏
if (pickedReturnPoint) {
showReturnPointTooltip(tooltipRef.current, movement.endPosition, pickedReturnPoint);
} else if (pickedRobot) {
const { lon, lat } = getCursorLngLat(viewer, movement.endPosition);
showCoordinateTooltip(tooltipRef.current, movement.endPosition, t, lon, lat, pickedRobot);
} else {
@@ -2000,11 +2222,11 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}
}
}
console.log('点击地图,拾取到设备', robot);
if (robot) {
onCameraClickRef.current?.(robot);
}
if (from !== "deviceStatus")
const _allowedFrom = ["mapEditor"];
if (!_allowedFrom.includes(from))
return;
const cartesian = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);
if (!cartesian) return;
@@ -2596,6 +2818,66 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
whiteSpace: "nowrap",
}}
/>
{/* 图例:返航点 / 分区 */}
{(stationOverlay.returnPoints.length > 0 || stationOverlay.regions.length > 0) && (
<div
style={{
position: "absolute",
right: 12,
top: 12,
zIndex: 20,
background: "rgba(255,255,255,0.92)",
borderRadius: 6,
padding: "8px 10px",
fontSize: 12,
color: "#333",
lineHeight: "18px",
boxShadow: "0 2px 8px rgba(0,0,0,0.12)",
}}
>
<div style={{ fontWeight: 600, marginBottom: 6 }}>{t('devices.mapEditor.legend')}</div>
<div style={{
display: "flex",
alignItems: "center",
gap: 6,
padding: "2px 4px",
borderRadius: 4,
fontWeight: legendHighlight === "returnPoint" ? 600 : 400,
color: legendHighlight === "returnPoint" ? "#165DFF" : "#333",
background: legendHighlight === "returnPoint" ? "#e8f3ff" : "transparent",
}}>
<span style={{
display: "inline-block",
width: 10,
height: 10,
background: "#165DFF",
borderRadius: "50% 50% 50% 0",
transform: "rotate(-45deg)",
}} />
<span>{t('devices.mapEditor.returnPoint')}</span>
</div>
<div style={{
display: "flex",
alignItems: "center",
gap: 6,
marginTop: 4,
padding: "2px 4px",
borderRadius: 4,
fontWeight: legendHighlight === "region" ? 600 : 400,
color: legendHighlight === "region" ? "#13C2C2" : "#333",
background: legendHighlight === "region" ? "rgba(19,194,194,0.12)" : "transparent",
}}>
<span style={{
display: "inline-block",
width: 12,
height: 10,
border: "1px solid #13C2C2",
background: "rgba(19,194,194,0.18)",
}} />
<span>{t('devices.mapEditor.restArea')}</span>
</div>
</div>
)}
</div>
);