From c57799f26cf06995629cfdee55bdd4a94452b320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Tue, 23 Jun 2015 15:20:50 -0700 Subject: Reimplemented fit to bounds The new implementation is now public and takes advantage of MGLCoordinateBounds. It is re-ported from `Camera.prototype.fitBounds()` in mapbox/mapbox-gl-js to ensure correct behavior. A new function, MGLCoordinateBoundsMake(), makes it easier to create an MGLCoordinateBounds for use with this method. --- src/mbgl/map/map.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index c25d307c6e..fd0701591b 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -146,6 +146,27 @@ void Map::setLatLngZoom(LatLng latLng, double zoom, Duration duration) { update(Update::Zoom); } +void Map::fitBounds(LatLngBounds bounds, Duration duration) { + // Zoom level calculation below assumes no rotation. + setBearing(0); + + // Calculate the center point, respecting the projection. + vec2 nePixel = pixelForLatLng(bounds.ne); + vec2 swPixel = pixelForLatLng(bounds.sw); + vec2 centerPixel = (nePixel + swPixel) * 0.5; + LatLng centerLatLng = latLngForPixel(centerPixel); + + // Calculate the zoom level. + double scaleX = getWidth() / (nePixel.x - swPixel.x); + double scaleY = getHeight() / (nePixel.y - swPixel.y); + double minZoom = getMinZoom(); + double maxZoom = getMaxZoom(); + double zoom = std::log2(getScale() * std::fmin(scaleX, scaleY)); + zoom = std::fmax(std::fmin(zoom, maxZoom), minZoom); + + setLatLngZoom(centerLatLng, zoom, duration); +} + void Map::resetZoom() { setZoom(0); } -- cgit v1.2.1