summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-06-25 21:00:38 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-06-25 21:54:57 -0700
commitbcbb56c1886ef1f369c50ea675a97c48718e78ce (patch)
treecfbc461e2723c7f18c4d746069a41a137159dc09 /src
parent1d11624efdf2d73a8adb43310c481969ead18d46 (diff)
downloadqtlocation-mapboxgl-bcbb56c1886ef1f369c50ea675a97c48718e78ce.tar.gz
Maintain rotation when fitting to bounds
Also fit to the rotated bounds. A little more verbose than necessary due to <http://stackoverflow.com/a/2357688/4585461>. ref mapbox/mapbox-gl-js#1338
Diffstat (limited to 'src')
-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);