segments: fit initial map view to all existing segments

This commit is contained in:
Davide Scaini
2026-05-13 19:41:59 +02:00
parent d82033fd84
commit c2c4cb9f3a
+17 -1
View File
@@ -66,7 +66,23 @@
map.on('mouseenter', 'seg-hit', () => { map.getCanvas().style.cursor = 'pointer'; });
map.on('mouseleave', 'seg-hit', () => { map.getCanvas().style.cursor = ''; });
fetchSegments();
// Initial view: fit to all existing segments, fall back to default center
fetch('/api/segments', { credentials: 'include' })
.then(r => r.ok ? r.json() : [])
.then((all: Segment[]) => {
if (all.length > 0) {
const lonMin = Math.min(...all.map(s => s.bbox[0]));
const latMin = Math.min(...all.map(s => s.bbox[1]));
const lonMax = Math.max(...all.map(s => s.bbox[2]));
const latMax = Math.max(...all.map(s => s.bbox[3]));
map.fitBounds([[lonMin, latMin], [lonMax, latMax]], { padding: 60, maxZoom: 14 });
segments = all;
updateMapSource();
} else {
fetchSegments();
}
})
.catch(() => fetchSegments());
});
map.on('moveend', () => {