feat(receiver): 持久化接收机发现端点并支持位置备注信息

This commit is contained in:
zyp
2026-11-30 09:29:33 +08:00
parent 1d6d7a277e
commit 5e761fc064
95 changed files with 15744 additions and 701 deletions
+322
View File
@@ -0,0 +1,322 @@
import * as ml from '/vendor/maplibre-gl.mjs';
ml.setWorkerUrl('/vendor/maplibre-gl-worker.mjs');
const COLLECTION = 'FeatureCollection';
function project(matrix, coordinate, width, height) {
const {x, y, z} = coordinate;
const w = matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15];
if (w <= 0) return null;
return [
(1 + (matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12]) / w) * width / 2,
(1 - (matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13]) / w) * height / 2
];
}
export function openLiveMap(container, options) {
const {center, zoom, basemap, onCamera, onError} = options;
const map = new ml.Map({
container, center, zoom: zoom - 1, pitch: options.pitch ?? 52, bearing: options.bearing ?? 0, maxPitch: 70, maxZoom: 17,
renderWorldCopies: false, attributionControl: false,
style: {
version: 8,
sources: {
base: {type:'raster', tiles:[basemap.tiles], tileSize:256, maxzoom:18, attribution:'天地图 © 自然资源部'},
labels: {type:'raster', tiles:[basemap.labels || basemap.tiles], tileSize:256, maxzoom:18},
fences: {type:'geojson', data:{type:COLLECTION, features:[]}}
},
layers: [
{id:'background', type:'background', paint:{'background-color':'#0a1827'}},
{id:'base', type:'raster', source:'base'},
{id:'labels', type:'raster', source:'labels', layout:{visibility:basemap.labels ? 'visible' : 'none'}},
{id:'fences', type:'fill', source:'fences', paint:{'fill-color':'#ff536e','fill-opacity':0.18}},
{id:'fence-outline', type:'line', source:'fences', paint:{'line-color':'#ff637a','line-width':2}}
]
}
});
map.addControl(new ml.NavigationControl(), 'bottom-left');
map.addControl(new ml.AttributionControl({compact:true}), 'bottom-right');
const lines = document.createElement('canvas');
lines.className = 'rid-live-3d-trails';
lines.setAttribute('aria-hidden', 'true');
const context = lines.getContext('2d');
const overlay = document.createElement('div');
overlay.className = 'rid-live-3d-markers';
const popup = document.createElement('div');
popup.className = 'rid-live-3d-popup';
popup.hidden = true;
const close = document.createElement('button');
close.type = 'button';
close.setAttribute('aria-label', '关闭地图详情');
close.textContent = '×';
const popupBody = document.createElement('div');
popup.append(close, popupBody);
overlay.append(popup);
map.getCanvasContainer().append(lines, overlay);
let active = true;
let updatingCamera = false;
let selected = null;
let projection;
const zonePopup = new ml.Popup({closeButton:true, className:'zoneMapPopup', maxWidth:'260px'});
let config = basemap;
let fences = [];
let entries = new Map();
const elements = new Map();
let trailPoints = new WeakMap();
let trackCoords = options.trackCoords;
function closePopup() {
selected = null;
popup.hidden = true;
map.triggerRepaint();
}
close.addEventListener('click', () => {
const button = elements.get(selected)?.button;
closePopup();
button?.focus();
});
function showTarget(id) {
selected = id;
map.triggerRepaint();
}
function toGround(point) {
return ml.MercatorCoordinate.fromLngLat(point);
}
function toAir(point, height) {
return ml.MercatorCoordinate.fromLngLat(point, Math.max(0, height ?? 0));
}
function trailPoint(raw) {
let point = trailPoints.get(raw);
if (!point) {
point = toAir(trackCoords(raw[0], raw[1]), raw[2]);
trailPoints.set(raw, point);
}
return point;
}
function markerFor(entry) {
const button = document.createElement('button');
button.type = 'button';
button.className = 'rid-live-3d-marker';
const visual = document.createElement('span');
const height = document.createElement('span');
height.className = 'rid-live-3d-height';
button.append(visual, height);
button.addEventListener('pointerdown', event => event.stopPropagation());
button.addEventListener('click', event => {
event.stopPropagation();
if (selected === entry.id) closePopup();
else showTarget(entry.id);
});
overlay.append(button);
return {button, visual, height, icon: null};
}
function updateEntries(next) {
entries = new Map();
for (const entry of next) {
entries.set(entry.id, {...entry, ground:toGround(entry.position), air:toAir(entry.position, entry.height),
pilotGround:entry.pilot ? toGround(entry.pilot) : null});
let element = elements.get(entry.id);
if (!element) elements.set(entry.id, element = markerFor(entry));
if (element.icon !== entry.icon) {
element.visual.replaceChildren(entry.icon?.cloneNode(true) || document.createTextNode(entry.label[0]));
element.icon = entry.icon;
element.plane = element.visual.querySelector('.rid-drone-plane');
}
element.button.setAttribute('aria-label', entry.label);
const value = entry.heightLabel || '';
if (element.height.textContent !== value) element.height.textContent = value;
element.height.hidden = !value;
}
for (const [id, element] of elements) {
if (entries.has(id)) continue;
element.button.remove();
elements.delete(id);
if (selected === id) closePopup();
}
map.triggerRepaint();
}
function paint(matrix) {
if (!active || !context || !matrix) return;
const width = container.clientWidth;
const height = container.clientHeight;
if (!width || !height) return;
projection = {matrix:matrix.slice(),width,height};
const scale = Math.min(window.devicePixelRatio || 1, 2);
const pixelsWide = Math.round(width * scale);
const pixelsHigh = Math.round(height * scale);
if (lines.width !== pixelsWide || lines.height !== pixelsHigh) {
lines.width = pixelsWide;
lines.height = pixelsHigh;
}
context.setTransform(scale, 0, 0, scale, 0, 0);
context.clearRect(0, 0, width, height);
context.lineJoin = context.lineCap = 'round';
let popupAnchor;
const screen = coordinate => project(matrix, coordinate, width, height);
for (const [id, entry] of entries) {
const element = elements.get(id);
const position = screen(entry.air);
const visible = position && position[0] >= -32 && position[0] <= width + 32
&& position[1] >= -30 && position[1] <= height + 30;
element.button.hidden = !visible;
if (visible) {
element.button.style.left = position[0] + 'px';
element.button.style.top = position[1] + 'px';
if (element.plane && Number.isFinite(entry.heading)) {
const angle = entry.heading * Math.PI / 180;
const distance = 20 * entry.air.meterInMercatorCoordinateUnits();
const ahead = screen({x:entry.air.x + Math.sin(angle) * distance,
y:entry.air.y - Math.cos(angle) * distance, z:entry.air.z});
if (ahead) {
const screenAngle = Math.atan2(ahead[0] - position[0], position[1] - ahead[1]) * 180 / Math.PI;
element.plane.style.transform = `rotate(${screenAngle}deg)`;
}
}
}
if (entry.trail) {
context.beginPath();
context.setLineDash([]);
context.strokeStyle = entry.trailColor || '#43efa0';
context.lineWidth = 2.5;
let previous;
for (const raw of entry.trail) {
const point = screen(trailPoint(raw));
if (!point) { previous = undefined; continue; }
if (previous && !raw[3]) {
context.lineTo(...point);
} else {
context.moveTo(...point);
}
previous = raw;
}
context.stroke();
}
if (entry.pilotGround && visible) {
const pilot = screen(entry.pilotGround);
if (pilot) {
context.beginPath();
context.strokeStyle = '#55d5ef';
context.lineWidth = 1.5;
context.setLineDash([5, 5]);
context.moveTo(...pilot);
context.lineTo(...position);
context.stroke();
}
}
if (entry.height > 0 && visible) {
const ground = screen(entry.ground);
if (ground) {
context.beginPath();
context.setLineDash([4, 4]);
context.strokeStyle = '#a6e5ff';
context.lineWidth = 1.5;
context.moveTo(...ground);
context.lineTo(...position);
context.stroke();
}
}
element.button.setAttribute('aria-expanded', String(selected === id));
if (selected === id && visible) popupAnchor = [position, entry.popup];
}
popup.hidden = !popupAnchor;
if (popupAnchor) {
const [position, content] = popupAnchor;
if (popupBody.dataset.content !== content) {
popupBody.innerHTML = content;
popupBody.dataset.content = content;
}
const half = Math.min(popup.offsetWidth / 2, width / 2);
popup.style.left = Math.max(half, Math.min(width - half, position[0])) + 'px';
popup.style.top = position[1] + 'px';
popup.classList.toggle('below', position[1] < popup.offsetHeight + 30);
}
}
function applyStyle() {
map.getSource('base')?.setTiles([config.tiles]);
if (config.labels) map.getSource('labels')?.setTiles([config.labels]);
if (map.getLayer('labels')) map.setLayoutProperty('labels','visibility', config.labels ? 'visible' : 'none');
map.getSource('fences')?.setData({type:COLLECTION, features:fences});
}
map.on('style.load', () => {
applyStyle();
map.addLayer({id:'rid-airspace', type:'custom', renderingMode:'3d', render(_gl, args) {
paint(args.defaultProjectionData?.mainMatrix || args.modelViewProjectionMatrix);
}});
});
map.on('moveend', () => {
if (active && !updatingCamera) onCamera(map.getCenter(), map.getZoom() + 1);
});
map.on('click', event => {
closePopup();
if (!map.getLayer('fences')) return;
const [zone] = map.queryRenderedFeatures(event.point, {layers:['fences']});
if (zone) {
zonePopup.setLngLat(event.lngLat)
.setDOMContent(options.createZonePopupContent(zone.properties))
.addTo(map);
}
});
map.on('webglcontextlost', onError);
function resize() {
if (!active) return;
updatingCamera = true;
try {
map.resize();
} finally {
updatingCamera = false;
}
}
return {
getCamera: () => ({pitch:map.getPitch(), bearing:map.getBearing()}),
showTarget,
setActive(value) {
active = value;
if (!active) zonePopup.remove();
resize();
},
resize,
destroy: () => map.remove(),
setView(center, zoom) {
const current = map.getCenter();
if (Math.abs(current.lng - center[0]) < 1e-8 && Math.abs(current.lat - center[1]) < 1e-8
&& Math.abs(map.getZoom() - zoom + 1) < 1e-6) return;
updatingCamera = true;
try { map.jumpTo({center, zoom:zoom - 1}); } finally { updatingCamera = false; }
},
setBasemap(value) {
if (config.tiles === value.tiles && config.labels === value.labels) return;
if (config.tiles !== value.tiles) trailPoints = new WeakMap();
config = value;
applyStyle();
},
setFences(value) {
fences = value;
zonePopup.remove();
map.getSource('fences')?.setData({type:COLLECTION, features:fences});
},
updateEntries,
metrics() {
const screen = coordinate => projection && project(projection.matrix, coordinate, projection.width, projection.height);
return {camera:{pitch:map.getPitch(), bearing:map.getBearing(), zoom:map.getZoom()+1}, size:[lines.width,lines.height],
positions:[...entries.values()].map(entry => ({
id:entry.id, heading:entry.heading, height:entry.height, altitude:entry.air.toAltitude(),
air:screen(entry.air), ground:screen(entry.ground),
tail:entry.trail?.length ? screen(trailPoint(entry.trail.at(-1))) : null
}))};
}
};
}