mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
The Protomaps basemap keeps a few attributes per POI beyond the name the map already labels: kind, kind_detail and elevation. Clicking a POI now opens a popup with those, plus the feature's coordinates (or the click location when the feature has no point). The same POI appears at several min_zoom levels within one tile, so features are deduped by name and kind. Map popups (this one and the existing pin popup) now follow the active theme's tokens instead of MapLibre's fixed white, covering light, dark, night and amber. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
68 lines
2.8 KiB
JavaScript
68 lines
2.8 KiB
JavaScript
// Popup content for a clicked map POI, built only from attributes already in the pmtiles file.
|
|
// The Protomaps basemap keeps name, kind, kind_detail and elevation; there is no address,
|
|
// phone or opening hours to show.
|
|
|
|
function escapeHtml(value) {
|
|
return String(value)
|
|
.replace(/&/g, "&")
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">")
|
|
.replace(/"/g, """)
|
|
.replace(/'/g, "'");
|
|
}
|
|
|
|
// "american_football" -> "American football"
|
|
function humanize(value) {
|
|
const s = String(value).replace(/_/g, " ").trim();
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
}
|
|
|
|
function formatCoordinates(lon, lat) {
|
|
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
|
|
return `${lat.toFixed(5)}, ${lon.toFixed(5)}`;
|
|
}
|
|
|
|
// Returns the popup HTML for the clicked POI features, or null when nothing useful was clicked.
|
|
// The same POI is often present at several min_zoom levels in one tile, so features are deduped
|
|
// by name and kind. Coordinates come from the feature's own point when it has one, otherwise
|
|
// from where the user clicked (clickLngLat is {lng, lat}).
|
|
export function poiPopupHtml(features, clickLngLat = null) {
|
|
const seen = new Set();
|
|
const items = [];
|
|
for (const feature of features || []) {
|
|
const p = feature?.properties || {};
|
|
const name = p.name || p["name:en"];
|
|
if (!name && !p.kind) continue;
|
|
const key = `${name}|${p.kind}`;
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
|
|
const rows = [];
|
|
if (p.kind) {
|
|
const detail = p.kind_detail && p.kind_detail !== "none" ? ` (${humanize(p.kind_detail)})` : "";
|
|
rows.push(`${humanize(p.kind)}${detail}`);
|
|
}
|
|
if (p.elevation !== undefined && p.elevation !== null && p.elevation !== ""
|
|
&& Number.isFinite(Number(p.elevation))) {
|
|
const meters = Math.round(Number(p.elevation));
|
|
const feet = Math.round(meters * 3.28084);
|
|
rows.push(`Elevation: ${meters} m / ${feet} ft`);
|
|
}
|
|
const geometry = feature?.geometry;
|
|
const point = geometry?.type === "Point" && Array.isArray(geometry.coordinates)
|
|
? geometry.coordinates : null;
|
|
const coordinates = point
|
|
? formatCoordinates(Number(point[0]), Number(point[1]))
|
|
: clickLngLat && formatCoordinates(Number(clickLngLat.lng), Number(clickLngLat.lat));
|
|
if (coordinates) rows.push(coordinates);
|
|
|
|
items.push(
|
|
`<div style="margin-bottom:6px">` +
|
|
`<strong>${escapeHtml(name || humanize(p.kind))}</strong>` +
|
|
rows.map(r => `<br/><small>${escapeHtml(r)}</small>`).join("") +
|
|
`</div>`
|
|
);
|
|
}
|
|
if (!items.length) return null;
|
|
return `<div style="font-size:13px;padding-right:12px">${items.join("")}</div>`;
|
|
}
|