feat(web-ui): 增强围栏地图交互与接收机状态显示

- 在 geofence-map-adapter.js 中添加了处理围栏顶点和圆心的拖动功能,提升用户交互体验。
- 实现了计算两点间距离的函数,以支持动态调整围栏半径。
- 更新了 receivers.js 中的状态显示逻辑,优化了设备状态的反馈信息,确保更准确的连接状态提示。
- 修改了 CSS 文件以改善界面样式,确保在不同主题下的视觉一致性。
This commit is contained in:
2026-10-31 21:43:49 +08:00
parent 3ea9cf32ee
commit 87d0ec8352
3 changed files with 40 additions and 21 deletions
+6 -3
View File
@@ -11,9 +11,12 @@ function ring(g){if(g.type==='polygon')return [...g.vertices,g.vertices[0]];
for(let i=0;i<=96;i++){const bearing=i/96*2*Math.PI,p=Math.asin(Math.sin(a)*Math.cos(delta)+Math.cos(a)*Math.sin(delta)*Math.cos(bearing));const q=b+Math.atan2(Math.sin(bearing)*Math.sin(delta)*Math.cos(a),Math.cos(delta)-Math.sin(a)*Math.sin(p));points.push([q*180/Math.PI,p*180/Math.PI]);}return points;
}
function feature(f,draft=false){return {type:'Feature',properties:{id:f.id,enabled:f.enabled,draft},geometry:{type:'Polygon',coordinates:[ring(f.geometry)]}};}
function metres(a,b){const dLat=(b[1]-a[1])*Math.PI/180,dLon=(b[0]-a[0])*Math.PI/180,la=a[1]*Math.PI/180,lb=b[1]*Math.PI/180,h=Math.sin(dLat/2)**2+Math.cos(la)*Math.cos(lb)*Math.sin(dLon/2)**2;return 6371008.8*2*Math.asin(Math.min(1,Math.sqrt(h)));}
function handle(point,label,apply){const element=document.createElement('div');element.className='gf-handle';element.setAttribute('aria-label',label);const marker=new ml.Marker({element,anchor:'center',offset:[0,0],draggable:true}).setLngLat(point).addTo(map);marker.on('dragend',()=>{const p=marker.getLngLat();apply([p.lng,p.lat]);});markers.push(marker);}
function handles(){for(const m of markers)m.remove();markers=[];if(!map||mode||!data.draft)return;
const g=data.draft.geometry,points=g.type==='circle'?[g.center]:g.vertices;
points.forEach((point,index)=>{const element=document.createElement('div');element.className='gf-handle';element.setAttribute('aria-label',g.type==='circle'?'拖动围栏圆心':`拖动围栏顶点 ${index+1}`);const marker=new ml.Marker({element,draggable:true}).setLngLat(point).addTo(map);marker.on('dragend',()=>{const p=marker.getLngLat(),geometry=structuredClone(g);marker.setLngLat(point);if(g.type==='circle')geometry.center=[p.lng,p.lat];else geometry.vertices[index]=[p.lng,p.lat];emit('rid-fence-geometry',geometry);});markers.push(marker);});
const g=data.draft.geometry;
if(g.type==='circle'){handle(g.center,'拖动围栏圆心',p=>{const geometry=structuredClone(g);geometry.center=p;emit('rid-fence-geometry',geometry);});handle(ring(g)[24],'拖动圆边调整半径',p=>{const geometry=structuredClone(g);geometry.radius_m=Math.min(1000000,Math.max(1,metres(g.center,p)));emit('rid-fence-geometry',geometry);});return;}
g.vertices.forEach((point,index)=>handle(point,`拖动围栏顶点 ${index+1}`,p=>{const geometry=structuredClone(g);geometry.vertices[index]=p;emit('rid-fence-geometry',geometry);}));
}
export function render(value){data=value||data;if(!map?.getSource('fences'))return;
const features=data.entries.filter(f=>f.id!==data.draft?.id).map(f=>feature(f));if(data.draft)features.push(feature(data.draft,true));
@@ -21,7 +24,7 @@ export function render(value){data=value||data;if(!map?.getSource('fences'))retu
}
export function targets(items){latestTargets=(items||[]).filter(t=>Number.isFinite(t.longitude)&&Number.isFinite(t.latitude));map?.getSource('targets')?.setData({type:'FeatureCollection',features:latestTargets.map(t=>({type:'Feature',properties:t,geometry:{type:'Point',coordinates:[t.longitude,t.latitude]}}))});}
export function open(p){if(map){map.resize();return;}kind=p.basemap==='road'?'road':'imagery';
try{map=new ml.Map({container:'geofence-map',center:[p.longitude,p.latitude],zoom:p.zoom??15,pitch:0,bearing:0,maxZoom:18,maxTileCacheSize:1024,style:{version:8,sources:{base:{type:'raster',tiles:[url(0)],tileSize:256,maxzoom:18},labels:{type:'raster',tiles:[url(1)],tileSize:256,maxzoom:18},targets:{type:'geojson',data:{type:'FeatureCollection',features:[]}},fences:{type:'geojson',data:{type:'FeatureCollection',features:[]}}},layers:[{id:'background',type:'background',paint:{'background-color':'#020b18'}},{id:'base',type:'raster',source:'base'},{id:'labels',type:'raster',source:'labels'},{id:'fence-fill',type:'fill',source:'fences',paint:{'fill-color':['case',['get','enabled'],'#5ba6ed','#8192a5'],'fill-opacity':['case',['get','draft'],.22,.10]}},{id:'fence-line',type:'line',source:'fences',paint:{'line-color':['case',['get','draft'],'#c5e3ff',['get','enabled'],'#7ec6ff','#8192a5'],'line-width':['case',['get','draft'],2.5,1.5]}},{id:'target-dots',type:'circle',source:'targets',paint:{'circle-radius':6,'circle-color':['match',['get','list_kind'],'white','#83dcb9','black','#ff938b','#91c9ff'],'circle-stroke-color':'#020b18','circle-stroke-width':2}}]}});
try{map=new ml.Map({container:'geofence-map',center:[p.longitude,p.latitude],zoom:p.zoom??15,pitch:0,bearing:0,maxZoom:18,maxTileCacheSize:1024,boxZoom:false,dragRotate:false,style:{version:8,sources:{base:{type:'raster',tiles:[url(0)],tileSize:256,maxzoom:18},labels:{type:'raster',tiles:[url(1)],tileSize:256,maxzoom:18},targets:{type:'geojson',data:{type:'FeatureCollection',features:[]}},fences:{type:'geojson',data:{type:'FeatureCollection',features:[]}}},layers:[{id:'background',type:'background',paint:{'background-color':'#020b18'}},{id:'base',type:'raster',source:'base'},{id:'labels',type:'raster',source:'labels'},{id:'fence-fill',type:'fill',source:'fences',paint:{'fill-color':['case',['get','enabled'],'#5ba6ed','#8192a5'],'fill-opacity':['case',['get','draft'],.22,.10]}},{id:'fence-line',type:'line',source:'fences',layout:{'line-join':'round','line-cap':'round'},paint:{'line-color':['case',['get','draft'],'#d7ecff',['get','enabled'],'#7ec6ff','#8192a5'],'line-width':['case',['get','draft'],2.5,1.5]}},{id:'target-dots',type:'circle',source:'targets',paint:{'circle-radius':6,'circle-color':['match',['get','list_kind'],'white','#83dcb9','black','#ff938b','#91c9ff'],'circle-stroke-color':'#020b18','circle-stroke-width':2}}]}});
buffer=attachTileBuffer(map,()=>kind);observer=new ResizeObserver(()=>map?.resize());observer.observe(document.getElementById('geofence-map'));map.addControl(new ml.NavigationControl({showCompass:false}),'bottom-left');map.addControl(new ml.AttributionControl({customAttribution:'天地图 © 自然资源部 · 本地监测围栏',compact:true}),'bottom-right');
map.on('load',()=>{render(data);targets(latestTargets);});map.on('error',e=>{if(!['base','labels'].includes(e.sourceId))emit('rid-fence-map-error',{message:'围栏地图操作失败,请使用坐标编辑并检查 WebGL。'});});
map.on('click',e=>{if(mode==='circle'){const radius=data.draft?.geometry?.radius_m||500;emit('rid-fence-geometry',{type:'circle',center:[e.lngLat.lng,e.lngLat.lat],radius_m:radius});tool('');}