]> jcornell.net Git - ntbd-parcels.git/commitdiff
Fly map to bounds of results after rendering
authorJakob Cornell <jakob+gpg@jcornell.net>
Thu, 21 May 2026 04:28:23 +0000 (23:28 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Thu, 21 May 2026 04:28:23 +0000 (23:28 -0500)
main.js

diff --git a/main.js b/main.js
index 39ae14d681667d3b2d00f5b681ec3acbb18c73b6..8c70cbb5030040b1951b5cbce745921c85eceede 100644 (file)
--- 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");