Skip to content

Commit 4c0f1d6

Browse files
author
Spike Brehm
authored
Merge pull request #954 from RajkumarPunchh/Collout-onPress-fix
Fixed issue #286.
2 parents eccf876 + f5bb3df commit 4c0f1d6

File tree

3 files changed

+53
-40
lines changed

3 files changed

+53
-40
lines changed

ios/AirMaps/AIRMapMarker.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- (BOOL)shouldShowCalloutView;
4949
- (void)showCalloutView;
5050
- (void)hideCalloutView;
51+
- (void)addTapGestureRecognizer;
5152

5253
@end
5354

ios/AirMaps/AIRMapMarker.m

+51
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ - (MKAnnotationView *)getAnnotationView
7171
// In this case, we want to render a platform "default" marker.
7272
if (_pinView == nil) {
7373
_pinView = [[MKPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
74+
[self addGestureRecognizerToView:_pinView];
7475
_pinView.annotation = self;
7576
}
7677

@@ -167,6 +168,56 @@ - (void)showCalloutView
167168
animated:YES];
168169
}
169170

171+
#pragma mark - Tap Gesture & Events.
172+
173+
- (void)addTapGestureRecognizer {
174+
[self addGestureRecognizerToView:nil];
175+
}
176+
177+
- (void)addGestureRecognizerToView:(UIView *)view {
178+
if (!view) {
179+
view = self;
180+
}
181+
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)];
182+
// setting this to NO allows the parent MapView to continue receiving marker selection events
183+
tapGestureRecognizer.cancelsTouchesInView = NO;
184+
[view addGestureRecognizer:tapGestureRecognizer];
185+
}
186+
187+
- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
188+
AIRMapMarker *marker = self;
189+
if (!marker) return;
190+
191+
if (marker.selected) {
192+
CGPoint touchPoint = [recognizer locationInView:marker.map.calloutView];
193+
if ([marker.map.calloutView hitTest:touchPoint withEvent:nil]) {
194+
195+
// the callout got clicked, not the marker
196+
id event = @{
197+
@"action": @"callout-press",
198+
};
199+
200+
if (marker.onCalloutPress) marker.onCalloutPress(event);
201+
if (marker.calloutView && marker.calloutView.onPress) marker.calloutView.onPress(event);
202+
if (marker.map.onCalloutPress) marker.map.onCalloutPress(event);
203+
return;
204+
}
205+
}
206+
207+
// the actual marker got clicked
208+
id event = @{
209+
@"action": @"marker-press",
210+
@"id": marker.identifier ?: @"unknown",
211+
@"coordinate": @{
212+
@"latitude": @(marker.coordinate.latitude),
213+
@"longitude": @(marker.coordinate.longitude)
214+
}
215+
};
216+
217+
if (marker.onPress) marker.onPress(event);
218+
if (marker.map.onMarkerPress) marker.map.onMarkerPress(event);
219+
}
220+
170221
- (void)hideCalloutView
171222
{
172223
// hide the callout view

ios/AirMaps/AIRMapMarkerManager.m

+1-40
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ @implementation AIRMapMarkerManager
2525
- (UIView *)view
2626
{
2727
AIRMapMarker *marker = [AIRMapMarker new];
28-
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)];
29-
// setting this to NO allows the parent MapView to continue receiving marker selection events
30-
tapGestureRecognizer.cancelsTouchesInView = NO;
31-
[marker addGestureRecognizer:tapGestureRecognizer];
28+
[marker addTapGestureRecognizer];
3229
marker.bridge = self.bridge;
3330
return marker;
3431
}
@@ -78,40 +75,4 @@ - (UIView *)view
7875
}];
7976
}
8077

81-
#pragma mark - Events
82-
83-
- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
84-
AIRMapMarker *marker = (AIRMapMarker *)recognizer.view;
85-
if (!marker) return;
86-
87-
if (marker.selected) {
88-
CGPoint touchPoint = [recognizer locationInView:marker.map.calloutView];
89-
if ([marker.map.calloutView hitTest:touchPoint withEvent:nil]) {
90-
91-
// the callout got clicked, not the marker
92-
id event = @{
93-
@"action": @"callout-press",
94-
};
95-
96-
if (marker.onCalloutPress) marker.onCalloutPress(event);
97-
if (marker.calloutView && marker.calloutView.onPress) marker.calloutView.onPress(event);
98-
if (marker.map.onCalloutPress) marker.map.onCalloutPress(event);
99-
return;
100-
}
101-
}
102-
103-
// the actual marker got clicked
104-
id event = @{
105-
@"action": @"marker-press",
106-
@"id": marker.identifier ?: @"unknown",
107-
@"coordinate": @{
108-
@"latitude": @(marker.coordinate.latitude),
109-
@"longitude": @(marker.coordinate.longitude)
110-
}
111-
};
112-
113-
if (marker.onPress) marker.onPress(event);
114-
if (marker.map.onMarkerPress) marker.map.onMarkerPress(event);
115-
}
116-
11778
@end

0 commit comments

Comments
 (0)