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

Commit

Permalink
Dismiss selected callout when tapping elsewhere
Browse files Browse the repository at this point in the history
Annotation views shouldn’t be mapped by annotation because they aren’t stable enough (we also want to support multiple annotations in one location etc.).
  • Loading branch information
1ec5 committed Mar 7, 2015
1 parent 36fc979 commit 9c46c6c
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ - (id)rawStyle;
@end

@implementation MGLMapView {
NSMutableDictionary *_annotationViewsByAnnotation;
NSMutableArray *_annotationViews;
UIImage *_pinImage;
}

Expand Down Expand Up @@ -293,7 +293,7 @@ - (BOOL)commonInit
[container addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
[self addSubview:container];

_annotationViewsByAnnotation = [NSMutableDictionary dictionary];
_annotationViews = [NSMutableArray array];
_pinImage = [[self class] resourceImageNamed:@"pin"];

self.annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAnnotationTapGesture:)];
Expand Down Expand Up @@ -328,6 +328,13 @@ - (BOOL)commonInit
[twoFingerTap requireGestureRecognizerToFail:_rotate];
[self addGestureRecognizer:twoFingerTap];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tap.numberOfTouchesRequired = 1;
[tap requireGestureRecognizerToFail:doubleTap];
[tap requireGestureRecognizerToFail:self.annotationTap];
tap.delegate = self;
[self addGestureRecognizer:tap];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
_quickZoom = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleQuickZoomGesture:)];
Expand Down Expand Up @@ -694,6 +701,12 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
}
}

- (void)handleTapGesture:(UITapGestureRecognizer *)tap {
(void)tap;
MGLAnnotationView *annotationView = [self viewForAnnotation:self.selectedAnnotation];
[annotationView.calloutView dismissCalloutAnimated:YES];
}

- (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
{
if ( ! self.isZoomEnabled) return;
Expand Down Expand Up @@ -1385,14 +1398,14 @@ - (NSDictionary *)allowedStyleTypes
#pragma mark - Annotations -

- (NSArray *)annotations {
return [_annotationViewsByAnnotation allKeys];
return [_annotationViews valueForKey:@"annotation"];
}

- (void)addAnnotation:(id <MGLAnnotation>)annotation {
MGLAnnotationView *annotationView = [[MGLAnnotationView alloc] initWithImage:_pinImage];
annotationView.annotation = annotation;
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];
[_annotationViewsByAnnotation setObject:annotationView forKey:annotation];
[_annotationViews addObject:annotationView];
[self.glView addSubview:annotationView];
[annotationView addGestureRecognizer:self.annotationTap];
[self updateAnnotation:annotation];
Expand All @@ -1405,8 +1418,9 @@ - (void)addAnnotations:(NSArray *)annotations {
}

- (void)removeAnnotation:(id <MGLAnnotation>)annotation {
[[_annotationViewsByAnnotation objectForKey:annotation] removeFromSuperview];
[_annotationViewsByAnnotation removeObjectForKey:annotation];
MGLAnnotationView *annotationView = [self viewForAnnotation:annotation];
[annotationView removeFromSuperview];
[_annotationViews removeObject:annotationView];
}

- (void)removeAnnotations:(NSArray *)annotations {
Expand All @@ -1416,7 +1430,12 @@ - (void)removeAnnotations:(NSArray *)annotations {
}

- (MGLAnnotationView *)viewForAnnotation:(id <MGLAnnotation>)annotation {
return _annotationViewsByAnnotation[annotation];
NSArray *candidateAnnotationViews = [_annotationViews filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
(void)bindings;
return [evaluatedObject annotation] == annotation;
}]];
// TODO: Cycle through the candidates.
return [candidateAnnotationViews firstObject];
}

- (void)updateAnnotations {
Expand All @@ -1433,7 +1452,7 @@ - (void)updateAnnotations {
}

- (void)updateAnnotation:(id <MGLAnnotation>)annotation {
MGLAnnotationView *annotationView = [_annotationViewsByAnnotation objectForKey:annotation];
MGLAnnotationView *annotationView = [self viewForAnnotation:annotation];
CGPoint center = [self convertCoordinate:annotationView.annotation.coordinate toPointToView:self];
if (CGRectContainsPoint(self.bounds, center)) {
annotationView.center = center;
Expand Down

0 comments on commit 9c46c6c

Please sign in to comment.