From 89190f8d891f7734a3db3267afb746cf83e872d5 Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Thu, 11 Aug 2016 09:28:24 -0700 Subject: [PATCH] [ios] Account for center offset when tapping annotation views (#5931) This adjusts the tap point for the map's single tap gesture recognizer to use the coordinate to point translated point of the actual annotation the view represents. This fixes an issue where views that were offset in the point coordinate system were not selected when tapped since their associated annotations were actually at the origin of the offset. --- platform/ios/ios.xcodeproj/project.pbxproj | 4 ++++ .../ios/src/MGLAnnotationContainerView_Private.h | 14 ++++++++++++++ platform/ios/src/MGLMapView.mm | 13 +++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 platform/ios/src/MGLAnnotationContainerView_Private.h diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index d6a741f33..1bdc3e7a7 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 4018B1C91CDC288A00F666AF /* MGLAnnotationView_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C31CDC277F00F666AF /* MGLAnnotationView_Private.h */; }; 4018B1CA1CDC288E00F666AF /* MGLAnnotationView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4018B1CB1CDC288E00F666AF /* MGLAnnotationView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 404326891D5B9B27007111BD /* MGLAnnotationContainerView_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 404326881D5B9B1A007111BD /* MGLAnnotationContainerView_Private.h */; }; 40EDA1C01CFE0E0200D9EA68 /* MGLAnnotationContainerView.h in Headers */ = {isa = PBXBuildFile; fileRef = 40EDA1BD1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.h */; }; 40EDA1C11CFE0E0500D9EA68 /* MGLAnnotationContainerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 40EDA1BE1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.m */; }; 40EDA1C21CFE0E0500D9EA68 /* MGLAnnotationContainerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 40EDA1BE1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.m */; }; @@ -341,6 +342,7 @@ 4018B1C41CDC277F00F666AF /* MGLAnnotationView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLAnnotationView.mm; sourceTree = ""; }; 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAnnotationView.h; sourceTree = ""; }; 402E9DE01CD2C76200FD4519 /* Mapbox.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = Mapbox.playground; sourceTree = ""; }; + 404326881D5B9B1A007111BD /* MGLAnnotationContainerView_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLAnnotationContainerView_Private.h; sourceTree = ""; }; 40EDA1BD1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLAnnotationContainerView.h; sourceTree = ""; }; 40EDA1BE1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLAnnotationContainerView.m; sourceTree = ""; }; 40FDA7691CCAAA6800442548 /* MBXAnnotationView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBXAnnotationView.h; sourceTree = ""; }; @@ -929,6 +931,7 @@ isa = PBXGroup; children = ( 40EDA1BD1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.h */, + 404326881D5B9B1A007111BD /* MGLAnnotationContainerView_Private.h */, 40EDA1BE1CFE0D4A00D9EA68 /* MGLAnnotationContainerView.m */, 4018B1C51CDC277F00F666AF /* MGLAnnotationView.h */, 4018B1C31CDC277F00F666AF /* MGLAnnotationView_Private.h */, @@ -977,6 +980,7 @@ DA88482C1CBAFA6200AB86E3 /* NSBundle+MGLAdditions.h in Headers */, DA88488E1CBB047F00AB86E3 /* reachability.h in Headers */, DA8848231CBAFA6200AB86E3 /* MGLOfflineStorage_Private.h in Headers */, + 404326891D5B9B27007111BD /* MGLAnnotationContainerView_Private.h in Headers */, DA88483B1CBAFB8500AB86E3 /* MGLCalloutView.h in Headers */, DA8848531CBAFB9800AB86E3 /* MGLCompactCalloutView.h in Headers */, DA8847FB1CBAFA5100AB86E3 /* MGLShape.h in Headers */, diff --git a/platform/ios/src/MGLAnnotationContainerView_Private.h b/platform/ios/src/MGLAnnotationContainerView_Private.h new file mode 100644 index 000000000..007b03550 --- /dev/null +++ b/platform/ios/src/MGLAnnotationContainerView_Private.h @@ -0,0 +1,14 @@ +#import "MGLAnnotationContainerView.h" +#import "MGLAnnotationView.h" + +@class MGLAnnotationView; + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLAnnotationContainerView (Private) + +@property (nonatomic) NS_MUTABLE_ARRAY_OF(MGLAnnotationView *) *annotationViews; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 9ef042ad3..09eb2319a 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -42,6 +42,7 @@ #import "MGLMapboxEvents.h" #import "MGLCompactCalloutView.h" #import "MGLAnnotationContainerView.h" +#import "MGLAnnotationContainerView_Private.h" #import #import @@ -1407,6 +1408,18 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) } CGPoint tapPoint = [singleTap locationInView:self]; + + // Handle the case of an offset annotation view by converting the tap point to be the geo location + // of the annotation itself that the view represents + for (MGLAnnotationView *view in self.annotationContainerView.annotationViews) + { + if (view.centerOffset.dx != 0 || view.centerOffset.dy != 0) { + if (CGRectContainsPoint(view.frame, tapPoint)) { + CGPoint annotationPoint = [self convertCoordinate:view.annotation.coordinate toPointToView:self]; + tapPoint = annotationPoint; + } + } + } MGLAnnotationTag hitAnnotationTag = [self annotationTagAtPoint:tapPoint persistingResults:YES]; if (hitAnnotationTag != MGLAnnotationTagNotFound)