// Map rendering only. History queries and playback state live in Rust. import * as ml from './vendor/maplibre-gl.mjs'; import {attachTileBuffer} from './map-tile-buffer.js'; ml.setWorkerUrl('/vendor/maplibre-gl-worker.mjs'); let map,buffer,observer,marker,points=[],index=0,fitPending=false,kind='imagery'; const layers={imagery:['img_w','cia_w'],road:['vec_w','cva_w']}; const url=i=>`/api/maps/tile/${layers[kind][i]}/{z}/{x}/{y}`; function coordinate(point){ const lon=point?.longitude,lat=point?.latitude; return Number.isFinite(lon)&&Number.isFinite(lat)&&Math.abs(lon)<=180&&Math.abs(lat)<=90?[lon,lat]:null; } function paint(){ const source=map?.getSource('history-track'); if(!source)return; const segments=[];let segment=[]; for(const point of points){ const position=coordinate(point); if(!position)continue; if(point.segmentStart&&segment.length){if(segment.length>1)segments.push(segment);segment=[];} segment.push(position); } if(segment.length>1)segments.push(segment); source.setData({type:'FeatureCollection',features:segments.map(coordinates=>({type:'Feature',properties:{},geometry:{type:'LineString',coordinates}}))}); paintMarker(); if(fitPending&&points.length){ const bounds=new ml.LngLatBounds(); for(const point of points){const position=coordinate(point);if(position)bounds.extend(position);} if(!bounds.isEmpty())map.fitBounds(bounds,{padding:60,maxZoom:16,duration:0}); fitPending=false; } } function paintMarker(){ const position=coordinate(points[index]); if(!map||!position){marker?.remove();marker=null;return;} if(!marker)marker=new ml.Marker({color:'#82c5ff',anchor:'center'}).setLngLat(position).addTo(map); else marker.setLngLat(position); } export function open(p){ if(map){map.resize();return;} kind=p.basemap==='road'?'road':'imagery'; try{ map=new ml.Map({container:'history-map',center:[p.longitude,p.latitude],zoom:p.zoom??15,pitch:0,bearing:0,maxZoom:18,maxTileCacheSize:1024,attributionControl: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}}, layers:[{id:'background',type:'background',paint:{'background-color':'#061221'}},{id:'base',type:'raster',source:'base'},{id:'labels',type:'raster',source:'labels'}]}}); buffer=attachTileBuffer(map,()=>kind);observer=new ResizeObserver(()=>map?.resize());observer.observe(document.getElementById('history-map')); map.addControl(new ml.NavigationControl({showCompass:false}),'bottom-right'); map.addControl(new ml.AttributionControl({customAttribution:'天地图 © 自然资源部 · 历史轨迹',compact:true}),'bottom-right'); map.on('style.load',()=>{map.addSource('history-track',{type:'geojson',data:{type:'FeatureCollection',features:[]}}); map.addLayer({id:'history-track',type:'line',source:'history-track',paint:{'line-color':'#80c4ff','line-width':4,'line-opacity':.95}});paint();}); map.on('error',e=>{if(!['base','labels'].includes(e.sourceId))window.dispatchEvent(new Event('rid-history-map-error'));}); }catch{close();window.dispatchEvent(new Event('rid-history-map-error'));} } export function setPoints(value){points=Array.isArray(value)?value:[];index=0;fitPending=true;paint();} export function setIndex(value){index=Math.max(0,Math.min(points.length-1,Number(value)||0));paintMarker();} export function close(){buffer?.destroy();buffer=null;observer?.disconnect();observer=null;marker?.remove();marker=null;map?.remove();map=null;} export function metrics(){return {ready:!!map?.getSource('history-track'),points:points.length,index};}