Skip to content

Commit

Permalink
Merge pull request #954 from RajkumarPunchh/Collout-onPress-fix
Browse files Browse the repository at this point in the history
Fixed issue #286.
  • Loading branch information
Spike Brehm committed Feb 1, 2017
2 parents eccf876 + f5bb3df commit 4c0f1d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
1 change: 1 addition & 0 deletions ios/AirMaps/AIRMapMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- (BOOL)shouldShowCalloutView;
- (void)showCalloutView;
- (void)hideCalloutView;
- (void)addTapGestureRecognizer;

@end

Expand Down
51 changes: 51 additions & 0 deletions ios/AirMaps/AIRMapMarker.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ - (MKAnnotationView *)getAnnotationView
// In this case, we want to render a platform "default" marker.
if (_pinView == nil) {
_pinView = [[MKPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
[self addGestureRecognizerToView:_pinView];
_pinView.annotation = self;
}

Expand Down Expand Up @@ -167,6 +168,56 @@ - (void)showCalloutView
animated:YES];
}

#pragma mark - Tap Gesture & Events.

- (void)addTapGestureRecognizer {
[self addGestureRecognizerToView:nil];
}

- (void)addGestureRecognizerToView:(UIView *)view {
if (!view) {
view = self;
}
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)];
// setting this to NO allows the parent MapView to continue receiving marker selection events
tapGestureRecognizer.cancelsTouchesInView = NO;
[view addGestureRecognizer:tapGestureRecognizer];
}

- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
AIRMapMarker *marker = self;
if (!marker) return;

if (marker.selected) {
CGPoint touchPoint = [recognizer locationInView:marker.map.calloutView];
if ([marker.map.calloutView hitTest:touchPoint withEvent:nil]) {

// the callout got clicked, not the marker
id event = @{
@"action": @"callout-press",
};

if (marker.onCalloutPress) marker.onCalloutPress(event);
if (marker.calloutView && marker.calloutView.onPress) marker.calloutView.onPress(event);
if (marker.map.onCalloutPress) marker.map.onCalloutPress(event);
return;
}
}

// the actual marker got clicked
id event = @{
@"action": @"marker-press",
@"id": marker.identifier ?: @"unknown",
@"coordinate": @{
@"latitude": @(marker.coordinate.latitude),
@"longitude": @(marker.coordinate.longitude)
}
};

if (marker.onPress) marker.onPress(event);
if (marker.map.onMarkerPress) marker.map.onMarkerPress(event);
}

- (void)hideCalloutView
{
// hide the callout view
Expand Down
41 changes: 1 addition & 40 deletions ios/AirMaps/AIRMapMarkerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ @implementation AIRMapMarkerManager
- (UIView *)view
{
AIRMapMarker *marker = [AIRMapMarker new];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)];
// setting this to NO allows the parent MapView to continue receiving marker selection events
tapGestureRecognizer.cancelsTouchesInView = NO;
[marker addGestureRecognizer:tapGestureRecognizer];
[marker addTapGestureRecognizer];
marker.bridge = self.bridge;
return marker;
}
Expand Down Expand Up @@ -78,40 +75,4 @@ - (UIView *)view
}];
}

#pragma mark - Events

- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
AIRMapMarker *marker = (AIRMapMarker *)recognizer.view;
if (!marker) return;

if (marker.selected) {
CGPoint touchPoint = [recognizer locationInView:marker.map.calloutView];
if ([marker.map.calloutView hitTest:touchPoint withEvent:nil]) {

// the callout got clicked, not the marker
id event = @{
@"action": @"callout-press",
};

if (marker.onCalloutPress) marker.onCalloutPress(event);
if (marker.calloutView && marker.calloutView.onPress) marker.calloutView.onPress(event);
if (marker.map.onCalloutPress) marker.map.onCalloutPress(event);
return;
}
}

// the actual marker got clicked
id event = @{
@"action": @"marker-press",
@"id": marker.identifier ?: @"unknown",
@"coordinate": @{
@"latitude": @(marker.coordinate.latitude),
@"longitude": @(marker.coordinate.longitude)
}
};

if (marker.onPress) marker.onPress(event);
if (marker.map.onMarkerPress) marker.map.onMarkerPress(event);
}

@end

0 comments on commit 4c0f1d6

Please sign in to comment.