summaryrefslogtreecommitdiff
path: root/src/mbgl/map/map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mbgl/map/map.cpp')
-rw-r--r--src/mbgl/map/map.cpp34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp
index c8ae711199..bd6d0960dd 100644
--- a/src/mbgl/map/map.cpp
+++ b/src/mbgl/map/map.cpp
@@ -147,31 +147,39 @@ void Map::setLatLngZoom(LatLng latLng, double zoom, Duration duration) {
}
void Map::fitBounds(LatLngBounds bounds, EdgeInsets padding, Duration duration) {
- // Zoom level calculation below assumes no rotation.
- setBearing(0);
+ // Calculate the bounds of the possibly rotated `bounds` parameter with respect to the viewport.
+ vec2<> nwPixel = pixelForLatLng({bounds.ne.latitude, bounds.sw.longitude});
+ vec2<> swPixel = pixelForLatLng(bounds.sw);
+ vec2<> sePixel = pixelForLatLng({bounds.sw.latitude, bounds.ne.longitude});
+ vec2<> nePixel = pixelForLatLng(bounds.ne);
+ vec2<> visualBounds = {
+ (std::max(std::max(nwPixel.x, swPixel.x),
+ std::max(sePixel.x, nePixel.x)) -
+ std::min(std::min(nwPixel.x, swPixel.x),
+ std::min(sePixel.x, nePixel.x))),
+ (std::max(std::max(nwPixel.y, swPixel.y),
+ std::max(sePixel.y, nePixel.y)) -
+ std::min(std::min(nwPixel.y, swPixel.y),
+ std::min(sePixel.y, nePixel.y))),
+ };
// Calculate the zoom level.
- vec2<double> nePixel = pixelForLatLng(bounds.ne);
- vec2<double> swPixel = pixelForLatLng(bounds.sw);
- 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 scaleX = (getWidth() - padding.left - padding.right) / visualBounds.x;
+ double scaleY = (getHeight() - padding.top - padding.bottom) / visualBounds.y;
double minScale = std::fmin(scaleX, scaleY);
double zoom = std::log2(getScale() * minScale);
- zoom = std::fmax(std::fmin(zoom, maxZoom), minZoom);
+ zoom = std::fmax(std::fmin(zoom, getMaxZoom()), getMinZoom());
// Calculate the center point of a virtual bounds that is extended in all directions by padding.
- vec2<double> paddedNEPixel = {
+ vec2<> paddedNEPixel = {
nePixel.x + padding.right / minScale,
nePixel.y + padding.top / minScale,
};
- vec2<double> paddedSWPixel = {
+ vec2<> paddedSWPixel = {
swPixel.x - padding.left / minScale,
swPixel.y - padding.bottom / minScale,
};
- vec2<double> centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5;
+ vec2<> centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5;
LatLng centerLatLng = latLngForPixel(centerPixel);
setLatLngZoom(centerLatLng, zoom, duration);