summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-06-24 12:21:03 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-06-25 21:54:57 -0700
commit68e404c522d318ca87e5435a997a21a9604ad0e8 (patch)
treecdfa27548d06f17b6de31468a339534c886e459f /src
parentc57799f26cf06995629cfdee55bdd4a94452b320 (diff)
downloadqtlocation-mapboxgl-68e404c522d318ca87e5435a997a21a9604ad0e8.tar.gz
Fit to bounds with padding
Each side of the bounding box is specified independently, allowing more flexibility than the offset + padding construct supported in mapbox/mapbox-gl-js’ Camera.
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/map/map.cpp28
1 files changed, 19 insertions, 9 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index fd0701591b..c8ae711199 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -146,24 +146,34 @@ void Map::setLatLngZoom(LatLng latLng, double zoom, Duration duration) {
update(Update::Zoom);
}
-void Map::fitBounds(LatLngBounds bounds, Duration duration) {
+void Map::fitBounds(LatLngBounds bounds, EdgeInsets padding, Duration duration) {
// Zoom level calculation below assumes no rotation.
setBearing(0);
- // Calculate the center point, respecting the projection.
+ // Calculate the zoom level.
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);
+ vec2<double> size = nePixel - swPixel;
+ double scaleX = (getWidth() - padding.left - padding.right) / size.x;
+ double scaleY = (getHeight() - padding.top - padding.bottom) / size.y;
double minZoom = getMinZoom();
double maxZoom = getMaxZoom();
- double zoom = std::log2(getScale() * std::fmin(scaleX, scaleY));
+ double minScale = std::fmin(scaleX, scaleY);
+ double zoom = std::log2(getScale() * minScale);
zoom = std::fmax(std::fmin(zoom, maxZoom), minZoom);
+ // Calculate the center point of a virtual bounds that is extended in all directions by padding.
+ vec2<double> paddedNEPixel = {
+ nePixel.x + padding.right / minScale,
+ nePixel.y + padding.top / minScale,
+ };
+ vec2<double> paddedSWPixel = {
+ swPixel.x - padding.left / minScale,
+ swPixel.y - padding.bottom / minScale,
+ };
+ vec2<double> centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5;
+ LatLng centerLatLng = latLngForPixel(centerPixel);
+
setLatLngZoom(centerLatLng, zoom, duration);
}