[gnome-maps/wip/mlundblad/replace-bbox-and-ensure-visible: 1/4] mapView: Add method to get fitting zoom level




commit 315bc3ec8bf220fe3e414f38e31353ee968451ca
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Feb 22 22:41:20 2021 +0100

    mapView: Add method to get fitting zoom level
    
    Add a method to get the lowest zoom level above the
    current level that fits a given bounding box at the
    current position.

 src/mapView.js | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
---
diff --git a/src/mapView.js b/src/mapView.js
index 60bf1785..0d407c01 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -711,6 +711,33 @@ var MapView = GObject.registerClass({
         new MapWalker.MapWalker(place, this).goTo(true, linear);
     }
 
+    getZoomLevelFittingBBox(bbox) {
+        let mapSource = this.view.get_map_source();
+        let goodSize = false;
+        let zoomLevel = this.view.max_zoom_level;
+
+        do {
+
+            let minX = mapSource.get_x(zoomLevel, bbox.left);
+            let minY = mapSource.get_y(zoomLevel, bbox.bottom);
+            let maxX = mapSource.get_x(zoomLevel, bbox.right);
+            let maxY = mapSource.get_y(zoomLevel, bbox.top);
+
+            if (minY - maxY <= this.view.height &&
+                maxX - minX <= this.view.width)
+                goodSize = true;
+            else
+                zoomLevel--;
+
+            if (zoomLevel <= this.view.min_zoom_level) {
+                zoomLevel = this.view.min_zoom_level;
+                goodSize = true;
+            }
+        } while (!goodSize);
+
+        return zoomLevel;
+    }
+
     showTurnPoint(turnPoint) {
         if (this._turnPointMarker)
             this._turnPointMarker.destroy();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]