Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Added delegate method to restrict movement
Browse files Browse the repository at this point in the history
Added a way for the delegate to restrict where the user can move within the map using gestures.

Fixes #2457.
  • Loading branch information
1ec5 committed Jul 10, 2016
1 parent d076612 commit 11253ec
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Mapbox welcomes participation and contributions from everyone. Please read [CON

## master

* Added a method to MGLMapViewDelegate, `-mapView:shouldChangeFromCamera:toCamera:`, that you can implement to restrict which parts the user can navigate to using gestures. ([#5584](https://github.com/mapbox/mapbox-gl-native/pull/5584))

## 3.3.0

### Styles and data
Expand Down
10 changes: 10 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan

_mbglMap->cancelTransitions();

MGLMapCamera *oldCamera = self.camera;
BOOL didChangeCamera = NO;
if (pan.state == UIGestureRecognizerStateBegan)
{
[self trackGestureEvent:MGLEventGesturePanStart forRecognizer:pan];
Expand All @@ -1202,6 +1204,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
CGPoint delta = [pan translationInView:pan.view];
_mbglMap->moveBy({ delta.x, delta.y });
[pan setTranslation:CGPointZero inView:pan.view];
didChangeCamera = YES;

[self notifyMapChange:mbgl::MapChangeRegionIsChanging];
}
Expand Down Expand Up @@ -1234,6 +1237,13 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
MGLEventKeyZoomLevel: @(zoom)
}];
}

if (didChangeCamera
&& [self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera])
{
self.camera = oldCamera;
}
}

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
Expand Down
19 changes: 19 additions & 0 deletions platform/ios/src/MGLMapViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

/**
Asks the delegate whether the map view should be allowed to change from the
existing camera to the new camera in response to a user gesture.
This method is called as soon as the user gesture is recognized. It is not
called in response to a programmatic camera change, such as by setting the
`centerCoordinate` property or calling `-flyToCamera:completionHandler:`.
@param mapView The map view that the user is manipulating.
@param oldCamera The camera representing the viewpoint at the moment the
gesture is recognized. If this method returns `NO`, the map view’s camera
continues to be this camera.
@param newCamera The expected camera after the gesture completes. If this
method returns `YES`, this camera becomes the map view’s camera.
@return A Boolean value indicating whether the map view should stay at
`oldCamera` or change to `newCamera`.
*/
- (BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera *)oldCamera toCamera:(MGLMapCamera *)newCamera;

#pragma mark Loading the Map

/**
Expand Down
1 change: 1 addition & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## master

* Right-clicking to open MGLMapView’s context menu no longer prevents the user from subsequently panning the map by clicking and dragging. ([#5593](https://github.com/mapbox/mapbox-gl-native/pull/5593))
* Added a method to MGLMapViewDelegate, `-mapView:shouldChangeFromCamera:toCamera:`, that you can implement to restrict which parts the user can navigate to using gestures. ([#5584](https://github.com/mapbox/mapbox-gl-native/pull/5584))
* Replaced the wireframe debug mask with an overdraw visualization debug mask to match Mapbox GL JS’s overdraw inspector. ([#5403](https://github.com/mapbox/mapbox-gl-native/pull/5403))
* Improved the design of the generated API documentation. ([#5306](https://github.com/mapbox/mapbox-gl-native/pull/5306))

Expand Down
32 changes: 32 additions & 0 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,13 @@ - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate animated:(B
- (void)offsetCenterCoordinateBy:(NSPoint)delta animated:(BOOL)animated {
[self willChangeValueForKey:@"centerCoordinate"];
_mbglMap->cancelTransitions();
MGLMapCamera *oldCamera = self.camera;
_mbglMap->moveBy({ delta.x, delta.y },
MGLDurationInSeconds(animated ? MGLAnimationDuration : 0));
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
[self didChangeValueForKey:@"centerCoordinate"];
}

Expand Down Expand Up @@ -949,8 +954,13 @@ - (void)zoomBy:(double)zoomDelta animated:(BOOL)animated {
- (void)scaleBy:(double)scaleFactor atPoint:(NSPoint)point animated:(BOOL)animated {
[self willChangeValueForKey:@"centerCoordinate"];
[self willChangeValueForKey:@"zoomLevel"];
MGLMapCamera *oldCamera = self.camera;
mbgl::ScreenCoordinate center(point.x, self.bounds.size.height - point.y);
_mbglMap->scaleBy(scaleFactor, center, MGLDurationInSeconds(animated ? MGLAnimationDuration : 0));
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
[self didChangeValueForKey:@"zoomLevel"];
[self didChangeValueForKey:@"centerCoordinate"];
}
Expand Down Expand Up @@ -1285,15 +1295,25 @@ - (void)handlePanGesture:(NSPanGestureRecognizer *)gestureRecognizer {
_directionAtBeginningOfGesture = self.direction;
_pitchAtBeginningOfGesture = _mbglMap->getPitch();
} else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) {
MGLMapCamera *oldCamera = self.camera;
BOOL didChangeCamera = NO;
mbgl::ScreenCoordinate center(startPoint.x, self.bounds.size.height - startPoint.y);
if (self.rotateEnabled) {
CLLocationDirection newDirection = _directionAtBeginningOfGesture - delta.x / 10;
[self willChangeValueForKey:@"direction"];
_mbglMap->setBearing(newDirection, center);
didChangeCamera = YES;
[self didChangeValueForKey:@"direction"];
}
if (self.pitchEnabled) {
_mbglMap->setPitch(_pitchAtBeginningOfGesture + delta.y / 5, center);
didChangeCamera = YES;
}

if (didChangeCamera
&& [self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
}
} else if (self.scrollEnabled) {
Expand Down Expand Up @@ -1333,7 +1353,12 @@ - (void)handleMagnificationGesture:(NSMagnificationGestureRecognizer *)gestureRe
if (gestureRecognizer.magnification > -1) {
[self willChangeValueForKey:@"zoomLevel"];
[self willChangeValueForKey:@"centerCoordinate"];
MGLMapCamera *oldCamera = self.camera;
_mbglMap->setScale(_scaleAtBeginningOfGesture * (1 + gestureRecognizer.magnification), center);
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
[self didChangeValueForKey:@"centerCoordinate"];
[self didChangeValueForKey:@"zoomLevel"];
}
Expand Down Expand Up @@ -1408,9 +1433,16 @@ - (void)handleRotationGesture:(NSRotationGestureRecognizer *)gestureRecognizer {
_mbglMap->setGestureInProgress(true);
_directionAtBeginningOfGesture = self.direction;
} else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) {
MGLMapCamera *oldCamera = self.camera;

NSPoint rotationPoint = [gestureRecognizer locationInView:self];
mbgl::ScreenCoordinate center(rotationPoint.x, self.bounds.size.height - rotationPoint.y);
_mbglMap->setBearing(_directionAtBeginningOfGesture + gestureRecognizer.rotationInDegrees, center);

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
} else if (gestureRecognizer.state == NSGestureRecognizerStateEnded
|| gestureRecognizer.state == NSGestureRecognizerStateCancelled) {
_mbglMap->setGestureInProgress(false);
Expand Down
19 changes: 19 additions & 0 deletions platform/macos/src/MGLMapViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)mapView:(MGLMapView *)mapView cameraDidChangeAnimated:(BOOL)animated;

/**
Asks the delegate whether the map view should be allowed to change from the
existing camera to the new camera in response to a user gesture.

This method is called as soon as the user gesture is recognized. It is not
called in response to a programmatic camera change, such as by setting the
`centerCoordinate` property or calling `-flyToCamera:completionHandler:`.

@param mapView The map view that the user is manipulating.
@param oldCamera The camera representing the viewpoint at the moment the
gesture is recognized. If this method returns `NO`, the map view’s camera
continues to be this camera.
@param newCamera The expected camera after the gesture completes. If this
method returns `YES`, this camera becomes the map view’s camera.
@return A Boolean value indicating whether the map view should stay at
`oldCamera` or change to `newCamera`.
*/
- (BOOL)mapView:(MGLMapView *)mapView shouldChangeFromCamera:(MGLMapCamera *)oldCamera toCamera:(MGLMapCamera *)newCamera;

#pragma mark Loading the Map

/**
Expand Down

0 comments on commit 11253ec

Please sign in to comment.