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.
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -38,6 +38,10 @@ using AnnotationIDs = std::vector<uint32_t>;
|
||||
using AnnotationSegment = std::vector<LatLng>;
|
||||
using AnnotationSegments = std::vector<AnnotationSegment>;
|
||||
|
||||
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;
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user