优化 focuslocation变化的时候 同一位置出现俩机器的bug

This commit is contained in:
mmc
2026-09-20 13:51:44 +08:00
parent 844216c525
commit 92978da4a9

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 } from 'react';
import carImg from '../assets/images/carplane.png';
import inspectImg from "../assets/images/inspectCar.png"
import planeImg from '../assets/images/planeicon.png';
@@ -91,6 +91,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
const realtimeLastPointRef = useRef(null);
const realtimeHiddenRef = useRef(false);
const focusEntityRef = useRef(null);
const hiddenByFocusRef = useRef([]);
const yellowCircleCanvas = useMemo(
@@ -1285,11 +1286,24 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
viewerRef.current?.entities.remove(focusEntityRef.current);
focusEntityRef.current = null;
}
// 恢复被 focusEntity 隐藏的设备实体
hiddenByFocusRef.current.forEach(e => {
if (e.billboard) e.billboard.show = true;
if (e.label) e.label.show = true;
});
hiddenByFocusRef.current = [];
return;
}
const viewer = viewerRef.current;
const Cesium = window.Cesium;
// 恢复上一次被隐藏的设备实体
hiddenByFocusRef.current.forEach(e => {
if (e.billboard) e.billboard.show = true;
if (e.label) e.label.show = true;
});
hiddenByFocusRef.current = [];
// 选择图片
let image = carImg;
if (focusLocation.type === "plane" || focusLocation.type === "drone") {
@@ -1305,6 +1319,24 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
viewer.entities.remove(focusEntityRef.current);
}
// 隐藏同位置的设备实体,避免重复显示(同一设备出现两个图标和 label)
const focusLon = Number(focusLocation.lon);
const focusLat = Number(focusLocation.lat);
entityMapRef.current.forEach((entity) => {
const robotData = entity.properties?.robot?.getValue?.();
if (!robotData) return;
const raw = robotData?.locationMessage || robotData?.lastRunningStatus || {};
const entLon = Number(raw.longitude);
const entLat = Number(raw.latitude);
if (!isFinite(entLon) || !isFinite(entLat)) return;
// 经纬度相近(容差 0.0001 度)则视为同一设备
if (Math.abs(entLon - focusLon) < 0.0001 && Math.abs(entLat - focusLat) < 0.0001) {
if (entity.billboard) entity.billboard.show = false;
if (entity.label) entity.label.show = false;
hiddenByFocusRef.current.push(entity);
}
});
// 添加新的 focusEntity
focusEntityRef.current = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(Number(focusLocation.lon), Number(focusLocation.lat), 0),