summaryrefslogtreecommitdiff
path: root/platform/ios
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2015-06-23 15:20:50 -0700
committerMinh Nguyễn <mxn@1ec5.org>2015-06-25 21:54:53 -0700
commitc57799f26cf06995629cfdee55bdd4a94452b320 (patch)
tree97ace2a4b513fbf92092343f3f747698c6c8c675 /platform/ios
parentf5c1e2630e553a727a91f0de623bf3ec1bdcc559 (diff)
downloadqtlocation-mapboxgl-c57799f26cf06995629cfdee55bdd4a94452b320.tar.gz
Reimplemented fit to bounds
The new implementation is now public and takes advantage of MGLCoordinateBounds. It is re-ported from `Camera.prototype.fitBounds()` in mapbox/mapbox-gl-js to ensure correct behavior. A new function, MGLCoordinateBoundsMake(), makes it easier to create an MGLCoordinateBounds for use with this method.
Diffstat (limited to 'platform/ios')
-rw-r--r--platform/ios/MGLMapView.mm34
1 files changed, 23 insertions, 11 deletions
diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm
index 4c014d12cf..b8fea9d8e8 100644
--- a/platform/ios/MGLMapView.mm
+++ b/platform/ios/MGLMapView.mm
@@ -1445,20 +1445,32 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
[self setZoomLevel:zoomLevel animated:NO];
}
-- (void)zoomToSouthWestCoordinate:(CLLocationCoordinate2D)southWestCoordinate northEastCoordinate:(CLLocationCoordinate2D)northEastCoordinate animated:(BOOL)animated
+MGLCoordinateBounds MGLCoordinateBoundsFromLatLngBounds(mbgl::LatLngBounds latLngBounds)
{
- // NOTE: does not disrupt tracking mode
+ return MGLCoordinateBoundsMake(MGLLocationCoordinate2DFromLatLng(latLngBounds.sw),
+ MGLLocationCoordinate2DFromLatLng(latLngBounds.ne));
+}
- CLLocationCoordinate2D center = CLLocationCoordinate2DMake((northEastCoordinate.latitude + southWestCoordinate.latitude) / 2, (northEastCoordinate.longitude + southWestCoordinate.longitude) / 2);
+mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coordinateBounds)
+{
+ return mbgl::LatLngBounds(MGLLatLngFromLocationCoordinate2D(coordinateBounds.sw), MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne));
+}
- CGFloat scale = _mbglMap->getScale();
- CGFloat scaleX = _mbglMap->getWidth() / (northEastCoordinate.longitude - southWestCoordinate.longitude);
- CGFloat scaleY = _mbglMap->getHeight() / (northEastCoordinate.latitude - southWestCoordinate.latitude);
- CGFloat minZoom = _mbglMap->getMinZoom();
- CGFloat maxZoom = _mbglMap->getMaxZoom();
- CGFloat zoomLevel = MAX(MIN(log(scale * MIN(scaleX, scaleY)) / log(2), maxZoom), minZoom);
+- (MGLCoordinateBounds)visibleCoordinateBounds
+{
+ return MGLCoordinateBoundsFromLatLngBounds(self.viewportBounds);
+}
- [self setCenterCoordinate:center zoomLevel:zoomLevel animated:animated];
+- (void)setVisibleCoordinateBounds:(MGLCoordinateBounds)bounds animated:(BOOL)animated
+{
+ // NOTE: does not disrupt tracking mode
+ CGFloat duration = animated ? MGLAnimationDuration : 0;
+
+ _mbglMap->fitBounds(MGLLatLngBoundsFromCoordinateBounds(bounds), secondsAsDuration(duration));
+
+ [self unrotateIfNeededAnimated:animated];
+
+ [self notifyMapChange:@(animated ? mbgl::MapChangeRegionDidChangeAnimated : mbgl::MapChangeRegionDidChange)];
}
- (CLLocationDirection)direction
@@ -2212,7 +2224,7 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng)
desiredSouthWest.longitude != actualSouthWest.longitude)
{
// assumes we won't disrupt tracking mode
- [self zoomToSouthWestCoordinate:desiredSouthWest northEastCoordinate:desiredNorthEast animated:YES];
+ [self setVisibleCoordinateBounds:MGLCoordinateBoundsMake(desiredSouthWest, desiredNorthEast) animated:YES];
}
}
}