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

Round tap-zoom gestures to nearest integer #8027

Merged
merged 4 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Map : private util::noncopyable {
void setScale(double scale, optional<ScreenCoordinate> = {}, const Duration& = Duration::zero());
double getScale() const;
void setZoom(double zoom, const Duration& = Duration::zero());
void setZoom(double zoom, optional<ScreenCoordinate>, const Duration& = Duration::zero());
void setZoom(double zoom, optional<EdgeInsets>, const Duration& = Duration::zero());
double getZoom() const;
void setLatLngZoom(const LatLng&, double zoom, const Duration& = Duration::zero());
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Added a `MGLDistanceFormatter` class for formatting geographic distances. ([#7888](https://github.com/mapbox/mapbox-gl-native/pull/7888))
* 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))
* Annotations are no longer deselected when the map is panned or zoomed, even if the annotation moves out of the visible bounds. ([#8022](https://github.com/mapbox/mapbox-gl-native/pull/8022))
* Double-tap and two-finger tap gestures now zoom to the nearest integer zoom level. ([#8027](https://github.com/mapbox/mapbox-gl-native/pull/8027))

## 3.4.1 - January 25, 2017

Expand Down
29 changes: 24 additions & 5 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) {
MBXSettingsMiscellaneousShowReuseQueueStats = 0,
MBXSettingsMiscellaneousWorldTour,
MBXSettingsMiscellaneousCustomUserDot,
MBXSettingsMiscellaneousShowZoomLevel,
MBXSettingsMiscellaneousPrintLogFile,
MBXSettingsMiscellaneousDeleteLogFile,
};
Expand Down Expand Up @@ -113,6 +114,7 @@ @interface MBXViewController () <UITableViewDelegate,
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;
@property (nonatomic) BOOL usingLocaleBasedCountryLabels;
@property (nonatomic) BOOL reuseQueueStatsEnabled;
@property (nonatomic) BOOL showZoomLevelEnabled;

@end

Expand Down Expand Up @@ -153,6 +155,7 @@ - (void)viewDidLoad
[self restoreState:nil];

self.debugLoggingEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsDebugLoggingEnabled"];

self.hudLabel.hidden = YES;

if ([MGLAccountManager accessToken].length)
Expand Down Expand Up @@ -339,11 +342,11 @@ - (void)dismissSettings:(__unused id)sender
]];
break;
case MBXSettingsMiscellaneous:
[settingsTitles addObject:@"Show Reuse Queue Stats"];

[settingsTitles addObjectsFromArray:@[
[NSString stringWithFormat:@"%@ Reuse Queue Stats", (_reuseQueueStatsEnabled ? @"Hide" :@"Show")],
@"Start World Tour",
[NSString stringWithFormat:@"%@ Custom User Dot", (_customUserLocationAnnnotationEnabled ? @"Disable" : @"Enable")],
[NSString stringWithFormat:@"%@ Zoom Level", (_showZoomLevelEnabled ? @"Hide" :@"Show")],
]];

if (self.debugLoggingEnabled)
Expand Down Expand Up @@ -522,8 +525,16 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
break;
case MBXSettingsMiscellaneousShowReuseQueueStats:
{
self.reuseQueueStatsEnabled = YES;
self.hudLabel.hidden = NO;
self.reuseQueueStatsEnabled = !self.reuseQueueStatsEnabled;
self.hudLabel.hidden = !self.reuseQueueStatsEnabled;
self.showZoomLevelEnabled = NO;
break;
}
case MBXSettingsMiscellaneousShowZoomLevel:
{
self.showZoomLevelEnabled = !self.showZoomLevelEnabled;
self.hudLabel.hidden = !self.showZoomLevelEnabled;
self.reuseQueueStatsEnabled = NO;
break;
}
default:
Expand Down Expand Up @@ -1711,7 +1722,15 @@ - (void)mapViewRegionIsChanging:(MGLMapView *)mapView
{
queuedAnnotations += queue.count;
}
self.hudLabel.text = [NSString stringWithFormat:@"Visible: %ld Queued: %ld", (unsigned long)mapView.visibleAnnotations.count, (unsigned long)queuedAnnotations];
self.hudLabel.text = [NSString stringWithFormat:@" Visible: %ld Queued: %ld", (unsigned long)mapView.visibleAnnotations.count, (unsigned long)queuedAnnotations];
} else if (self.showZoomLevelEnabled) {
self.hudLabel.text = [NSString stringWithFormat:@" Zoom: %.2f", self.mapView.zoomLevel];
}
}

- (void)mapView:(MGLMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
if (self.showZoomLevelEnabled) {
self.hudLabel.text = [NSString stringWithFormat:@" Zoom: %.2f", self.mapView.zoomLevel];
}
}

Expand Down
19 changes: 11 additions & 8 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1556,18 +1556,20 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
if (doubleTap.state == UIGestureRecognizerStateEnded)
{
MGLMapCamera *oldCamera = self.camera;


double newZoom = round(self.zoomLevel) + 1.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the corresponding changes to the Zoom In and Zoom Out buttons in the Android and macOS SDKs?

if (zoomIn) {
mapView.scaleBy(2.0, x, y, MapboxConstants.ANIMATION_DURATION);
} else {
mapView.scaleBy(0.5, x, y, MapboxConstants.ANIMATION_DURATION);
}

[self setZoomLevel:self.zoomLevel + zoomDelta animated:animated];

/cc @tobrun

Copy link
Contributor Author

@friedbunny friedbunny Feb 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went a bit further on macOS in 4ed80f9 and rounded all of the non-freeform zoom methods gestures and commands.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll leave Android to @tobrun, in light of #7630.


CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:self.zoomLevel + 1.0 aroundAnchorPoint:gesturePoint];

MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
[self trackGestureEvent:MGLEventGestureDoubleTap forRecognizer:doubleTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(2, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
_mbglMap->setZoom(newZoom, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down Expand Up @@ -1595,16 +1597,17 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
{
MGLMapCamera *oldCamera = self.camera;

double zoom = self.zoomLevel;
double newZoom = round(self.zoomLevel) - 1.0;

CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:zoom - 1.0 aroundAnchorPoint:gesturePoint];

MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
_mbglMap->setZoom(newZoom, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down
1 change: 1 addition & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* Fixed flickering that occurred when panning past the antimeridian. ([#7574](https://github.com/mapbox/mapbox-gl-native/pull/7574))
* 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))
* Added a `MGLDistanceFormatter` class for formatting geographic distances. ([#7888](https://github.com/mapbox/mapbox-gl-native/pull/7888))
* Zooming by double-tap, two-finger tap, zoom buttons, shortcut keys, or demo app menu items or shortcut keys now zooms to the nearest integer zoom level. ([#8027](https://github.com/mapbox/mapbox-gl-native/pull/8027))

## 0.3.1

Expand Down
4 changes: 2 additions & 2 deletions platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ - (IBAction)chooseCustomStyle:(id)sender {
}

- (IBAction)zoomIn:(id)sender {
[self.mapView setZoomLevel:self.mapView.zoomLevel + 1 animated:YES];
[self.mapView setZoomLevel:round(self.mapView.zoomLevel) + 1 animated:YES];
}

- (IBAction)zoomOut:(id)sender {
[self.mapView setZoomLevel:self.mapView.zoomLevel - 1 animated:YES];
[self.mapView setZoomLevel:round(self.mapView.zoomLevel) - 1 animated:YES];
}

- (IBAction)snapToNorth:(id)sender {
Expand Down
21 changes: 18 additions & 3 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,22 @@ - (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated {
}

- (void)zoomBy:(double)zoomDelta animated:(BOOL)animated {
[self setZoomLevel:self.zoomLevel + zoomDelta animated:animated];
[self setZoomLevel:round(self.zoomLevel) + zoomDelta animated:animated];
}

- (void)zoomBy:(double)zoomDelta atPoint:(NSPoint)point animated:(BOOL)animated {
[self willChangeValueForKey:@"centerCoordinate"];
[self willChangeValueForKey:@"zoomLevel"];
double newZoom = round(self.zoomLevel) + zoomDelta;
MGLMapCamera *oldCamera = self.camera;
mbgl::ScreenCoordinate center(point.x, self.bounds.size.height - point.y);
_mbglMap->setZoom(newZoom, center, MGLDurationInSecondsFromTimeInterval(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"];
}

- (void)scaleBy:(double)scaleFactor atPoint:(NSPoint)point animated:(BOOL)animated {
Expand Down Expand Up @@ -1500,7 +1515,7 @@ - (void)handleDoubleClickGesture:(NSClickGestureRecognizer *)gestureRecognizer {
_mbglMap->cancelTransitions();

NSPoint gesturePoint = [gestureRecognizer locationInView:self];
[self scaleBy:2 atPoint:gesturePoint animated:YES];
[self zoomBy:1 atPoint:gesturePoint animated:YES];
}

- (void)smartMagnifyWithEvent:(NSEvent *)event {
Expand All @@ -1512,7 +1527,7 @@ - (void)smartMagnifyWithEvent:(NSEvent *)event {

// Tap with two fingers (“right-click”) to zoom out on mice but not trackpads.
NSPoint gesturePoint = [self convertPoint:event.locationInWindow fromView:nil];
[self scaleBy:0.5 atPoint:gesturePoint animated:YES];
[self zoomBy:-1 atPoint:gesturePoint animated:YES];
}

/// Rotate fingers to rotate.
Expand Down
8 changes: 7 additions & 1 deletion src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,13 @@ double Map::getScale() const {

void Map::setZoom(double zoom, const Duration& duration) {
impl->cameraMutated = true;
setZoom(zoom, {}, duration);
setZoom(zoom, optional<EdgeInsets> {}, duration);
}

void Map::setZoom(double zoom, optional<ScreenCoordinate> anchor, const Duration& duration) {
impl->cameraMutated = true;
impl->transform.setZoom(zoom, anchor, duration);
impl->onUpdate(Update::RecalculateStyle);
}

void Map::setZoom(double zoom, optional<EdgeInsets> padding, const Duration& duration) {
Expand Down