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

Commit

Permalink
[ios] Introduce visible annotations API
Browse files Browse the repository at this point in the history
This adds an API for getting all annotations that are currently
visible on the map. In the future, an additional API can be added
to query by a specific rect. That API can be used internally to
power this one.

This also adds a new option to iosapp to test the feature. At the
time of this commit, the underlying `queryPointAnnotations` does
not appear to work as expected.
  • Loading branch information
boundsj committed Aug 17, 2016
1 parent bca9d09 commit c2a7ec8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ - (IBAction)showSettings:(__unused id)sender
((_customUserLocationAnnnotationEnabled)
? @"Disable Custom User Dot"
: @"Enable Custom User Dot"),
@"Query Annotations",
nil];

if (self.debugLoggingEnabled)
Expand Down Expand Up @@ -294,6 +295,10 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
self.mapView.showsUserLocation = NO;
self.mapView.userTrackingMode = MGLUserTrackingModeFollow;
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 18)
{
[self testQueryPointAnnotations];
}
else if (buttonIndex == actionSheet.numberOfButtons - 2 && self.debugLoggingEnabled)
{
NSString *fileContents = [NSString stringWithContentsOfFile:[self telemetryDebugLogfilePath] encoding:NSUTF8StringEncoding error:nil];
Expand Down Expand Up @@ -541,6 +546,20 @@ - (void)styleRoadLayer
roadLayer.minimumZoomLevel = 13;
}

- (void)testQueryPointAnnotations {
NSNumber *visibleAnnotationCount = @(self.mapView.visibleAnnotations.count);
NSString *message;
if ([visibleAnnotationCount integerValue] == 1) {
message = [NSString stringWithFormat:@"There is %@ visible annotation.", visibleAnnotationCount];
} else {
message = [NSString stringWithFormat:@"There are %@ visible annotations.", visibleAnnotationCount];
}

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Visible Annotations" message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan)
Expand Down
10 changes: 10 additions & 0 deletions platform/ios/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,16 @@ IB_DESIGNABLE
*/
@property (nonatomic, readonly, nullable) NS_ARRAY_OF(id <MGLAnnotation>) *annotations;

/**
The complete list of annotations associated with the receiver that are
currently visible. (read-only)
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 <MGLAnnotation>) *visibleAnnotations;

/**
Adds an annotation to the map view.
Expand Down
24 changes: 24 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2787,6 +2787,30 @@ - (void)removeStyleClass:(NSString *)styleClass
return [NSArray arrayWithObjects:&annotations[0] count:annotations.size()];
}

- (nullable NS_ARRAY_OF(id <MGLAnnotation>) *)visibleAnnotations
{
if (_annotationContextsByAnnotationTag.empty())
{
return nil;
}

std::vector<MGLAnnotationTag> 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 <MGLAnnotation>)annotationWithTag:(MGLAnnotationTag)tag
{
Expand Down

0 comments on commit c2a7ec8

Please sign in to comment.