diff --git a/platform/ios/app/MBXAnnotationView.m b/platform/ios/app/MBXAnnotationView.m index 890881a31..f3afe936d 100644 --- a/platform/ios/app/MBXAnnotationView.m +++ b/platform/ios/app/MBXAnnotationView.m @@ -25,4 +25,12 @@ } } +- (void)setSelected:(BOOL)selected animated:(BOOL)animated +{ + [super setSelected:selected animated:animated]; + + self.layer.borderColor = selected ? [UIColor blackColor].CGColor : [UIColor whiteColor].CGColor; + self.layer.borderWidth = selected ? 2.0 : 0; +} + @end diff --git a/platform/ios/src/MGLAnnotationView.h b/platform/ios/src/MGLAnnotationView.h index 5b8091e7b..acf270dfc 100644 --- a/platform/ios/src/MGLAnnotationView.h +++ b/platform/ios/src/MGLAnnotationView.h @@ -40,6 +40,26 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, assign, getter=isFlat) BOOL flat; +/** + Defaults to NO and becomes YES when the view is tapped on. + + Selecting another view will first deselect the currently selected view. + This property should not be changed directly. + */ +@property (nonatomic, assign, getter=isSelected) BOOL selected; + +/** + Subclasses may override this method in order to customize appearance. + This method should not be called directly. + */ +- (void)setSelected:(BOOL)selected animated:(BOOL)animated; + +/* + This property defaults to YES. Setting it to NO will cause the annotation view to ignore all touch events. + Subclasses may use this property to customize the appearance. + */ +@property (nonatomic, assign, getter=isEnabled) BOOL enabled; + /** Setting this property to YES will cause the annotation view to shrink as it approaches the horizon and grow as it moves away from the horizon when the associated map view is tilted. Conversely, setting this property to NO will ensure that the annotation view maintains diff --git a/platform/ios/src/MGLAnnotationView.mm b/platform/ios/src/MGLAnnotationView.mm index 31657dbf4..1a95beb23 100644 --- a/platform/ios/src/MGLAnnotationView.mm +++ b/platform/ios/src/MGLAnnotationView.mm @@ -22,6 +22,7 @@ { _reuseIdentifier = [reuseIdentifier copy]; _scalesWithViewingDistance = YES; + _enabled = YES; } return self; } @@ -37,6 +38,18 @@ self.center = self.center; } +- (void)setSelected:(BOOL)selected +{ + [self setSelected:selected animated:NO]; +} + +- (void)setSelected:(BOOL)selected animated:(BOOL)animated +{ + [self willChangeValueForKey:@"selected"]; + _selected = selected; + [self didChangeValueForKey:@"selected"]; +} + - (void)setCenter:(CGPoint)center { [self setCenter:center pitch:0]; diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 6549ca16e..90e018642 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -3382,17 +3382,22 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) // By default attempt to use the GL annotation image frame as the positioning rect. CGRect positioningRect = [self positioningRectForCalloutForAnnotationWithTag:annotationTag]; + MGLAnnotationView *annotationView = nil; + if (annotation != self.userLocation) { MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); - MGLAnnotationView *annotationView = annotationContext.annotationView; - if (annotationView) + annotationView = annotationContext.annotationView; + + if (annotationView && annotationView.enabled) { // Annotations represented by views use the view frame as the positioning rect. positioningRect = annotationView.frame; [annotationView.superview bringSubviewToFront:annotationView]; + + annotationView.selected = YES; } } @@ -3474,6 +3479,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { [self.delegate mapView:self didSelectAnnotation:annotation]; } + + if (annotationView && [self.delegate respondsToSelector:@selector(mapView:didSelectAnnotationView:)]) + { + [self.delegate mapView:self didSelectAnnotationView:annotationView]; + } } - (MGLCompactCalloutView *)calloutViewForAnnotation:(id )annotation @@ -3546,6 +3556,17 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { // dismiss popup [self.calloutViewForSelectedAnnotation dismissCalloutAnimated:animated]; + + // deselect annotation view + MGLAnnotationView *annotationView = nil; + MGLAnnotationTag annotationTag = [self annotationTagForAnnotation:annotation]; + + if (annotationTag != MGLAnnotationTagNotFound) + { + MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); + annotationView = annotationContext.annotationView; + annotationView.selected = NO; + } // clean up self.calloutViewForSelectedAnnotation = nil; @@ -3556,6 +3577,11 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { [self.delegate mapView:self didDeselectAnnotation:annotation]; } + + if (annotationView && [self.delegate respondsToSelector:@selector(mapView:didDeselectAnnotationView:)]) + { + [self.delegate mapView:self didDeselectAnnotationView:annotationView]; + } } } diff --git a/platform/ios/src/MGLMapViewDelegate.h b/platform/ios/src/MGLMapViewDelegate.h index 39eb43d4c..0833b7ace 100644 --- a/platform/ios/src/MGLMapViewDelegate.h +++ b/platform/ios/src/MGLMapViewDelegate.h @@ -279,6 +279,27 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)mapView:(MGLMapView *)mapView didDeselectAnnotation:(id )annotation; + +/** + Tells the delegate that one of its annotation views was selected. + + You can use this method to track changes in the selection state of annotation views. + + @param mapView The map view containing the annotation. + @param annotationView The annotation view that was selected. + */ +- (void)mapView:(MGLMapView *)mapView didSelectAnnotationView:(MGLAnnotationView *)annotationView; + +/** + Tells the delegate that one of its annotation views was deselected. + + You can use this method to track changes in the selection state of annotation views. + + @param mapView The map view containing the annotation. + @param annotationView The annotation view that was deselected. + */ +- (void)mapView:(MGLMapView *)mapView didDeselectAnnotationView:(MGLAnnotationView *)annotationView; + @end NS_ASSUME_NONNULL_END