From 9ed775748623333e41eb66ecf9ac5414c834948f Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Wed, 26 Oct 2016 15:04:24 -0700 Subject: [PATCH] [macos] Add visible annotations API --- platform/ios/src/MGLMapView.h | 2 +- platform/macos/src/MGLMapView.h | 10 ++++++++++ platform/macos/src/MGLMapView.mm | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/platform/ios/src/MGLMapView.h b/platform/ios/src/MGLMapView.h index 77f958f52da..b8ccf46e519 100644 --- a/platform/ios/src/MGLMapView.h +++ b/platform/ios/src/MGLMapView.h @@ -894,7 +894,7 @@ IB_DESIGNABLE /** The complete list of annotations associated with the receiver that are - currently visible. (read-only) + currently visible. The objects in this array must adopt the `MGLAnnotation` protocol. If no annotations are associated with the map view or if no annotations associated diff --git a/platform/macos/src/MGLMapView.h b/platform/macos/src/MGLMapView.h index 3499671ff1e..3cde4a888f0 100644 --- a/platform/macos/src/MGLMapView.h +++ b/platform/macos/src/MGLMapView.h @@ -568,6 +568,16 @@ IB_DESIGNABLE */ - (void)addAnnotations:(NS_ARRAY_OF(id ) *)annotations; +/** + The complete list of annotations associated with the receiver that are + currently visible. + + The objects in this array must adopt the `MGLAnnotation` protocol. If no + annotations are associated with the map view or if no annotations associated + with the map view are currently visible, the value of this property is `nil`. + */ +@property (nonatomic, readonly, nullable) NS_ARRAY_OF(id ) *visibleAnnotations; + /** Removes an annotation from the map view, deselecting it if it is selected. diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 14b83762a32..ee52de41ad9 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -1616,6 +1616,30 @@ - (IBAction)rotate:(NSSlider *)sender { return [NSArray arrayWithObjects:&annotations[0] count:annotations.size()]; } +- (nullable NS_ARRAY_OF(id ) *)visibleAnnotations +{ + if (_annotationContextsByAnnotationTag.empty()) + { + return nil; + } + + std::vector annotationTags = [self annotationTagsInRect:self.bounds]; + if (annotationTags.size()) + { + NSMutableArray *annotations = [NSMutableArray arrayWithCapacity:annotationTags.size()]; + + for (auto const& annotationTag: annotationTags) + { + MGLAnnotationContext annotationContext = _annotationContextsByAnnotationTag[annotationTag]; + [annotations addObject:annotationContext.annotation]; + } + + return [annotations copy]; + } + + return nil; +} + /// Returns the annotation assigned the given tag. Cheap. - (id )annotationWithTag:(MGLAnnotationTag)tag { if (!_annotationContextsByAnnotationTag.count(tag)) {