|
|
|
@@ -9,7 +9,7 @@ setWorkerCount(2)
|
|
|
|
|
|
|
|
|
|
export function createFamilyMap(container,{onSelect=()=>{},onStatus=()=>{},night=false}={}){
|
|
|
|
|
let map,disposed=false,people=[],track=[],selected='',markers=new globalThis.Map(),initialFit=false,available=false,theme=night,catalog={regions:[]},expiry,poll,resize,styleKey='',loadingCatalog=false
|
|
|
|
|
const abort=new AbortController()
|
|
|
|
|
const abort=new AbortController();let pulseTimer
|
|
|
|
|
const origin=location.origin
|
|
|
|
|
const chosen=(lon,lat)=>regionAt(catalog.regions||[],lon,lat)
|
|
|
|
|
const style=()=>{
|
|
|
|
@@ -36,7 +36,7 @@ export function createFamilyMap(container,{onSelect=()=>{},onStatus=()=>{},night
|
|
|
|
|
el.setAttribute('aria-label',`查看${p.name||'家人'}的位置`)
|
|
|
|
|
const avatar=document.createElement('span');avatar.textContent=String(p.name||'家人').slice(0,1)
|
|
|
|
|
const label=document.createElement('b');label.textContent=String(p.name||'家人');el.append(avatar,label)
|
|
|
|
|
el.onclick=()=>{selected=p.id;onSelect(p.id);focus(p.id)}
|
|
|
|
|
el.onclick=()=>{selected=p.id;onSelect(p.id);focus(p.id,true)}
|
|
|
|
|
markers.set(p.id,new Marker({element:el,anchor:'bottom'}).setLngLat([p.position.longitude,p.position.latitude]).addTo(map))
|
|
|
|
|
}
|
|
|
|
|
if(map.isStyleLoaded()){
|
|
|
|
@@ -54,8 +54,8 @@ export function createFamilyMap(container,{onSelect=()=>{},onStatus=()=>{},night
|
|
|
|
|
map.setMaxZoom(18)
|
|
|
|
|
map.fitBounds([[Math.min(...xs),Math.min(...ys)],[Math.max(...xs),Math.max(...ys)]],{padding:65,maxZoom:13,duration:500})
|
|
|
|
|
}
|
|
|
|
|
function focus(id){const p=visiblePeople(people).find(p=>p.id===id);if(!map||!p)return;selected=id;const pos=p.position,max=maximum(chosen(pos.longitude,pos.latitude));map.setMaxZoom(max);map.easeTo({center:[pos.longitude,pos.latitude],zoom:Math.min(14,max),duration:450});paint()}
|
|
|
|
|
const controller={setItems(items){people=Array.isArray(items)?items:[];if(!people.length){initialFit=false;selected=''}paint()},setTrack(points){track=Array.isArray(points)?points:[];paint();if(track.length)fit()},focus,fit,setNight(value){if(theme===value)return;theme=value;refreshStyle()},resize(){map?.resize()},destroy(){disposed=true;abort.abort();clearInterval(expiry);clearInterval(poll);resize?.disconnect();clearMarkers();map?.remove();map=null;people=[];track=[]},ready:Promise.resolve()}
|
|
|
|
|
function focus(id,pulse=false){const p=visiblePeople(people).find(p=>p.id===id);if(!map||!p)return;selected=id;const pos=p.position,max=maximum(chosen(pos.longitude,pos.latitude));map.setMaxZoom(max);map.easeTo({center:[pos.longitude,pos.latitude],zoom:Math.min(14,max),duration:450});paint();if(pulse){clearTimeout(pulseTimer);for(const marker of markers.values())marker.getElement().classList.remove('pulsing');const el=markers.get(id)?.getElement();if(el){void el.offsetWidth;el.classList.add('pulsing');pulseTimer=setTimeout(()=>el.classList.remove('pulsing'),3300)}}}
|
|
|
|
|
const controller={setItems(items){people=Array.isArray(items)?items:[];if(!people.length){initialFit=false;selected=''}paint()},setTrack(points){track=Array.isArray(points)?points:[];paint();if(track.length)fit()},focus,fit,setNight(value){if(theme===value)return;theme=value;refreshStyle()},resize(){map?.resize()},destroy(){disposed=true;abort.abort();clearTimeout(pulseTimer);clearInterval(expiry);clearInterval(poll);resize?.disconnect();clearMarkers();map?.remove();map=null;people=[];track=[]},ready:Promise.resolve()}
|
|
|
|
|
controller.ready=(async()=>{try{
|
|
|
|
|
const timeout=setTimeout(()=>abort.abort(),10000);let response;try{response=await fetch('/maps/catalog.json',{signal:abort.signal})}finally{clearTimeout(timeout)}if(!response.ok)throw Error();catalog=await response.json();if(disposed)return;if(!catalog.regions?.length){onStatus('地图数据尚未就绪,家人位置可查看列表');return}
|
|
|
|
|
map=new LibreMap({container,style:style(),center:[117.8,36.6],zoom:6,maxZoom:18,minZoom:1,attributionControl:false,renderWorldCopies:false,dragRotate:false,touchPitch:false,localIdeographFontFamily:'sans-serif'})
|
|
|
|
|