diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2015-06-23 15:20:50 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2015-06-25 21:54:53 -0700 |
commit | c57799f26cf06995629cfdee55bdd4a94452b320 (patch) | |
tree | 97ace2a4b513fbf92092343f3f747698c6c8c675 /src | |
parent | f5c1e2630e553a727a91f0de623bf3ec1bdcc559 (diff) | |
download | qtlocation-mapboxgl-c57799f26cf06995629cfdee55bdd4a94452b320.tar.gz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/map/map.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
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<double> nePixel = pixelForLatLng(bounds.ne); + vec2<double> swPixel = pixelForLatLng(bounds.sw); + vec2<double> 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); } |