From: Jakob Cornell Date: Thu, 21 May 2026 04:28:23 +0000 (-0500) Subject: Fly map to bounds of results after rendering X-Git-Url: https://jcornell.net/gitweb/gitweb.cgi?a=commitdiff_plain;h=9f323a9d5f2e623f11a94d597a492d0b85d829d0;p=ntbd-parcels.git Fly map to bounds of results after rendering --- 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");