From 68e404c522d318ca87e5435a997a21a9604ad0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Wed, 24 Jun 2015 12:21:03 -0700 Subject: [PATCH] Fit to bounds with padding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each side of the bounding box is specified independently, allowing more flexibility than the offset + padding construct supported in mapbox/mapbox-gl-js’ Camera. --- include/mbgl/ios/MGLMapView.h | 6 ++++++ include/mbgl/map/map.hpp | 6 +++++- platform/ios/MGLMapView.mm | 8 +++++++- src/mbgl/map/map.cpp | 28 +++++++++++++++++++--------- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/mbgl/ios/MGLMapView.h b/include/mbgl/ios/MGLMapView.h index 001e7aa67..bf42f47e7 100644 --- a/include/mbgl/ios/MGLMapView.h +++ b/include/mbgl/ios/MGLMapView.h @@ -126,6 +126,12 @@ IB_DESIGNABLE * @param animated Specify `YES` to animate the change by smoothly scrolling and zooming or `NO` to immediately display the given bounds. */ - (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds animated:(BOOL)animated; +/** Changes the receiver’s viewport to fit the given coordinate bounds, optionally animating the change. +* @param bounds The bounds that the viewport will show in its entirety. +* @param insets The minimum padding (in screen points) that will be visible around the given coordinate bounds. +* @param animated Specify `YES` to animate the change by smoothly scrolling and zooming or `NO` to immediately display the given bounds. */ +- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets animated:(BOOL)animated; + /** The heading of the map (measured in degrees) relative to true north. * * The value `0` means that the top edge of the map view corresponds to true north. The value `90` means the top of the map is pointing due east. The value `180` means the top of the map points due south, and so on. */ diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 42ae72bb7..463235915 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -38,6 +38,10 @@ using AnnotationIDs = std::vector; using AnnotationSegment = std::vector; using AnnotationSegments = std::vector; +using EdgeInsets = struct { + double top, left, bottom, right; +}; + class Map : private util::noncopyable { friend class View; @@ -95,7 +99,7 @@ public: void setZoom(double zoom, Duration = Duration::zero()); double getZoom() const; void setLatLngZoom(LatLng latLng, double zoom, Duration = Duration::zero()); - void fitBounds(LatLngBounds bounds, Duration = Duration::zero()); + void fitBounds(LatLngBounds bounds, EdgeInsets padding, Duration duration = Duration::zero()); void resetZoom(); double getMinZoom() const; double getMaxZoom() const; diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index b8fea9d8e..8fdfa294a 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -1462,11 +1462,17 @@ mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coord } - (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds animated:(BOOL)animated +{ + [self setVisibleCoordinateBounds:bounds edgePadding:UIEdgeInsetsZero animated:animated]; +} + +- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds edgePadding:(UIEdgeInsets)insets animated:(BOOL)animated { // NOTE: does not disrupt tracking mode CGFloat duration = animated ? MGLAnimationDuration : 0; - _mbglMap->fitBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), secondsAsDuration(duration)); + mbgl::EdgeInsets mbglInsets = {insets.top, insets.left, insets.bottom, insets.right}; + _mbglMap->fitBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), mbglInsets, secondsAsDuration(duration)); [self unrotateIfNeededAnimated:animated]; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index fd0701591..c8ae71119 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 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); + vec2 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 paddedNEPixel = { + nePixel.x + padding.right / minScale, + nePixel.y + padding.top / minScale, + }; + vec2 paddedSWPixel = { + swPixel.x - padding.left / minScale, + swPixel.y - padding.bottom / minScale, + }; + vec2 centerPixel = (paddedNEPixel + paddedSWPixel) * 0.5; + LatLng centerLatLng = latLngForPixel(centerPixel); + setLatLngZoom(centerLatLng, zoom, duration); }