wrolpi/app/src/components/mapPoiPopup.js
lrnselfreliance 177d0dba4b
The standalone map viewer draws overlays, as the map does (#966)
The standalone viewer (port 8084) drew basemaps only: a map file which is
not a basemap was skipped.  It now offers each overlay in its layer panel,
hidden until turned on, with a checkbox per layer, draws it with the style
beside it when there is one, and shows a feature's attributes on a click.

The overlay logic is the map viewer's own.  It moved to dependency-free
modules (app/src/map-shared) which a browser can load as they are; the
standalone viewer, which has no build step, loads copies of them from
modules/map/static/js.  A test fails while a copy differs from its module
(`npm run sync:map-shared` makes the copies).  What differs between the
viewers is passed in: where a map file and its style are fetched from, and
whether a layer can be checked before MapLibre is given it.

The standalone viewer cannot check a layer first, so it adds an overlay's
layers to the map one at a time once the style has loaded: MapLibre refuses a
whole style over one layer it cannot draw, but only that layer when it is
added by itself.

The API also names a style by its path within the map directory, which is
what the standalone viewer is served.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 17:28:41 -07:00

61 lines
2.6 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.
import {escapeHtml} from "../map-shared/overlays.js";
// "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>`;
}