Skip to content

Instantly share code, notes, and snippets.

@giulio92
Last active July 15, 2016 15:28
Show Gist options
  • Save giulio92/1cc878386d6a6a2c3bca70ca78136e1a to your computer and use it in GitHub Desktop.
Save giulio92/1cc878386d6a6a2c3bca70ca78136e1a to your computer and use it in GitHub Desktop.
Google Maps iOS SDK wrong GMSOverlay in didTapOverlay fix
#import <GoogleMaps/GoogleMaps.h>
@interface GMSMapView (DisableDoubleTapZoom)
- (void)setDoubleTapZoomEnabled:(BOOL)enabled;
@end
#import "GMSMapView+DisableDoubleTapZoom.h"
@implementation GMSMapView (DisableDoubleTapZoom)
- (void)setDoubleTapZoomEnabled:(BOOL)enabled {
for (id view in [self subviews]) {
if ([view isKindOfClass:NSClassFromString(@"GMSVectorMapView")] ) {
for (UITapGestureRecognizer *rec in [view gestureRecognizers]) {
NSRange range1 = [[rec description] rangeOfString:@"action=handleZoomTapGesture:"];
NSRange range2 = [[rec description] rangeOfString:@"action=handleSingleTapGesture:"];
if (range1.location && range2.location && range1.location < range2.location) {
[rec setEnabled:enabled];
break;
}
}
}
}
}
@end
@giulio92
Copy link
Author

giulio92 commented Jul 1, 2016

I have noticed that if you have a lot of GMSPolygons on your GMSMapView and you rely completely on Google Maps to determine if your GMSPolygon has been tapped or not it will (sometimes) give you the wrong GMSPolygon object in the following delegation method:

- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay

Because every GMSPolygons object has an "extra boundary" all around which helps the user select the polygon even with low levels of zoom. So if you have a lot of polygons in one restricted area (such as City regions) you will end up selecting the wrong one.

With this code we loop over the GMSMapView, looking for two UITapGestureRecognizerwhich are responsible for this problem:

  • handleZoomTapGesture
  • handleSingleTapGesture

and when we found them we set them disabled (instead of simply removing them which can cause compatibility issues in your GMSMapView)

There is only one thing you should keep in mind, since we disable the handleZoomTapGesture the user won't be able to double-tap-and-drag zoom again after you execute this code, so remember to re-enable those UITapGestureRecognizers at the end of the delegation method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment