From 9f323a9d5f2e623f11a94d597a492d0b85d829d0 Mon Sep 17 00:00:00 2001 From: Jakob Cornell Date: Wed, 20 May 2026 23:28:23 -0500 Subject: [PATCH] Fly map to bounds of results after rendering --- main.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/main.js b/main.js index 39ae14d..8c70cbb 100644 --- a/main.js +++ b/main.js @@ -86,7 +86,31 @@ async function asyncMain() { L.polygon(leafletCoords, {color: "blue", weight: 1}).addTo(map); } + // fly to a view that fits all matches in frame + let flyBounds = null; + if (matches.length) { + let minLat = Infinity; + let minLon = Infinity; + let maxLat = -Infinity; + let maxLon = -Infinity; + for (const feature of matches) { + // extract flat coordinates of all positive polygons + const coordinates = feature.geometry.coordinates.flatMap(poly => poly[0]); + const longitudes = coordinates.map(([lon, lat]) => lon); + const latitudes = coordinates.map(([lon, lat]) => lat); + minLat = Math.min(minLat, ...latitudes); + minLon = Math.min(minLon, ...longitudes); + maxLat = Math.max(maxLat, ...latitudes); + maxLon = Math.max(maxLon, ...longitudes); + } + flyBounds = L.latLngBounds(L.latLng(minLat, minLon), L.latLng(maxLat, maxLon)); + } + + console.log(flyBounds); overlay.style["display"] = "none"; + if (flyBounds !== null) { + map.flyToBounds(flyBounds); + } } const map = L.map("map"); -- 2.39.5