接入 esri底图 兜底方案
This commit is contained in:
@@ -37,6 +37,16 @@ const createTiandituProvider = (layerName) =>
|
||||
credit: '天地图',
|
||||
});
|
||||
|
||||
// 降级底图:天地图瓦片取不到时用 Esri 卫星影像兜底,避免地图黑屏
|
||||
const createEsriSatelliteProvider = () =>
|
||||
new Cesium.UrlTemplateImageryProvider({
|
||||
url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
|
||||
credit: 'Tiles © Esri',
|
||||
minimumLevel: 0,
|
||||
maximumLevel: 19,
|
||||
tilingScheme: new Cesium.WebMercatorTilingScheme(),
|
||||
});
|
||||
|
||||
|
||||
const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine, finishPoint, drawRealTime, realtimePosition, realtimeType, realtimeDevice, pathPoints = [], devices = [], onCameraClick, focusLocation, points = [], markedPoints = [], onPathChange, from, onClearDraw, onMarkPoint, routeMode = '', polyPoints, mapLayer, activeType, overlayRefreshKey = 0, focusOverlay = null, onObstacleRadiusChange, obstacleShapes = [], onObstacleRectResize, siteMapInfoId, detectReportInfo }, ref) => {
|
||||
const containerRef = useRef(null);
|
||||
@@ -67,7 +77,12 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
|
||||
// 天地图底图:矢量 / 卫星 切换
|
||||
const tiandituLayersRef = useRef(null);
|
||||
const [baseMapType, setBaseMapType] = useState('vector');
|
||||
const esriFallbackLayerRef = useRef(null);
|
||||
const [baseMapType, setBaseMapType] = useState('satellite');
|
||||
// 天地图不可用标记:瓦片连续加载失败后降级为 Esri 影像,并隐藏底图切换
|
||||
const [tiandituUnavailable, setTiandituUnavailable] = useState(false);
|
||||
const tiandituUnavailableRef = useRef(false);
|
||||
const tiandituErrorCountRef = useRef(0);
|
||||
|
||||
// 场站已配置的返航点 / 分区叠加显示
|
||||
const stationId = useSelector((state) => state?.station?.stationId || state?.station?.currentStation?.id);
|
||||
@@ -301,6 +316,32 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
tdtLayers.terrain.show = false;
|
||||
tdtLayers.terrainLabel.show = false;
|
||||
tiandituLayersRef.current = tdtLayers;
|
||||
|
||||
// Esri 影像置于最底层:天地图对应瓦片加载失败时会透出该图层,防止地图一片黑
|
||||
const esriFallbackLayer = viewer.imageryLayers.addImageryProvider(createEsriSatelliteProvider(), 0);
|
||||
esriFallbackLayer.show = true;
|
||||
esriFallbackLayerRef.current = esriFallbackLayer;
|
||||
|
||||
// 天地图瓦片加载失败累计超过阈值后判定不可用,后续只显示 Esri 影像
|
||||
const handleTiandituError = () => {
|
||||
if (tiandituUnavailableRef.current) return;
|
||||
tiandituErrorCountRef.current += 1;
|
||||
if (tiandituErrorCountRef.current >= 6) {
|
||||
tiandituUnavailableRef.current = true;
|
||||
setTiandituUnavailable(true);
|
||||
}
|
||||
};
|
||||
Object.values(tdtLayers).forEach((layer) => {
|
||||
layer.imageryProvider?.errorEvent?.addEventListener(handleTiandituError);
|
||||
});
|
||||
|
||||
// 主动探测天地图服务可达性:请求 0 级瓦片,失败即判定不可用
|
||||
const probeImg = new Image();
|
||||
probeImg.onerror = () => {
|
||||
tiandituUnavailableRef.current = true;
|
||||
setTiandituUnavailable(true);
|
||||
};
|
||||
probeImg.src = `https://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=0&TILEROW=0&TILECOL=0&tk=${TIANDITU_TK}`;
|
||||
viewer.scene.globe.baseColor = Cesium.Color.BLACK;
|
||||
viewer.scene.globe.depthTestAgainstTerrain = false;
|
||||
viewer.scene.requestRenderMode = true; // 👈 加这个(只在需要时渲染)
|
||||
@@ -321,6 +362,9 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
|
||||
return () => {
|
||||
if (lastFlightPromiseRef.current) viewer.camera.cancelFlight();
|
||||
Object.values(tdtLayers).forEach((layer) => {
|
||||
layer.imageryProvider?.errorEvent?.removeEventListener(handleTiandituError);
|
||||
});
|
||||
};
|
||||
}, []);
|
||||
|
||||
@@ -328,6 +372,17 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
useEffect(() => {
|
||||
const layers = tiandituLayersRef.current;
|
||||
if (!layers) return;
|
||||
|
||||
// 天地图不可用:隐藏全部天地图图层,只保留最底层的 Esri 影像
|
||||
if (tiandituUnavailable) {
|
||||
Object.values(layers).forEach((layer) => {
|
||||
layer.show = false;
|
||||
});
|
||||
if (esriFallbackLayerRef.current) esriFallbackLayerRef.current.show = true;
|
||||
viewerRef.current?.scene?.requestRender?.();
|
||||
return;
|
||||
}
|
||||
|
||||
const showVector = baseMapType === 'vector';
|
||||
const showSatellite = baseMapType === 'satellite';
|
||||
const showTerrain = baseMapType === 'terrain';
|
||||
@@ -338,7 +393,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
layers.terrain.show = showTerrain;
|
||||
layers.terrainLabel.show = showTerrain;
|
||||
viewerRef.current?.scene?.requestRender?.();
|
||||
}, [baseMapType]);
|
||||
}, [baseMapType, tiandituUnavailable]);
|
||||
|
||||
|
||||
|
||||
@@ -3880,42 +3935,44 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
/>
|
||||
{/* 底图切换:矢量 / 卫星 */}
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
right: 12,
|
||||
bottom: 12,
|
||||
zIndex: 20,
|
||||
display: 'flex',
|
||||
gap: 4,
|
||||
background: 'rgba(255,255,255,0.92)',
|
||||
borderRadius: 6,
|
||||
padding: 4,
|
||||
boxShadow: '0 2px 8px rgba(0,0,0,0.12)',
|
||||
}}
|
||||
>
|
||||
{[{ key: 'vector', label: t('common.mapVector') }, { key: 'satellite', label: t('common.mapSatellite') }, {
|
||||
key: 'terrain', label: t('common.mapTerrain')
|
||||
}].map((item) => (
|
||||
<button
|
||||
key={item.key}
|
||||
onClick={() => setBaseMapType(item.key)}
|
||||
style={{
|
||||
padding: '4px 10px',
|
||||
fontSize: 12,
|
||||
border: 'none',
|
||||
borderRadius: 4,
|
||||
cursor: 'pointer',
|
||||
background: baseMapType === item.key ? '#1677ff' : 'transparent',
|
||||
color: baseMapType === item.key ? '#fff' : '#333',
|
||||
transition: 'all 0.2s ease',
|
||||
}}
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{/* 底图切换:矢量 / 卫星 / 地形(天地图不可用时降级 Esri,隐藏切换) */}
|
||||
{!tiandituUnavailable && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
right: 12,
|
||||
bottom: 12,
|
||||
zIndex: 20,
|
||||
display: 'flex',
|
||||
gap: 4,
|
||||
background: 'rgba(255,255,255,0.92)',
|
||||
borderRadius: 6,
|
||||
padding: 4,
|
||||
boxShadow: '0 2px 8px rgba(0,0,0,0.12)',
|
||||
}}
|
||||
>
|
||||
{[{ key: 'vector', label: t('common.mapVector') }, { key: 'satellite', label: t('common.mapSatellite') }, {
|
||||
key: 'terrain', label: t('common.mapTerrain')
|
||||
}].map((item) => (
|
||||
<button
|
||||
key={item.key}
|
||||
onClick={() => setBaseMapType(item.key)}
|
||||
style={{
|
||||
padding: '4px 10px',
|
||||
fontSize: 12,
|
||||
border: 'none',
|
||||
borderRadius: 4,
|
||||
cursor: 'pointer',
|
||||
background: baseMapType === item.key ? '#1677ff' : 'transparent',
|
||||
color: baseMapType === item.key ? '#fff' : '#333',
|
||||
transition: 'all 0.2s ease',
|
||||
}}
|
||||
>
|
||||
{item.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{/* 图例:返航点 / 分区 */}
|
||||
{(stationOverlay.returnPoints.length > 0 || stationOverlay.regions.length > 0 || stationOverlay.tempObstaclePoints.length > 0) && (
|
||||
<div
|
||||
|
||||
Reference in New Issue
Block a user