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

Commit

Permalink
[WIP] Add visibleAnnotations API
Browse files Browse the repository at this point in the history
  • Loading branch information
boundsj committed Oct 17, 2016
1 parent a1d803f commit 861c066
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef NS_ENUM(NSInteger, MBXSettingsAnnotationsRows) {
MBXSettingsAnnotations10000Sprites,
MBXSettingsAnnotationsTestShapes,
MBXSettingsAnnotationsCustomCallout,
MBXSettingsAnnotationsQueryAnnotations,
MBXSettingsAnnotationsRemoveAnnotations,
};

Expand Down Expand Up @@ -98,6 +99,11 @@ @interface MBXViewController () <UITableViewDelegate,
@property (nonatomic) BOOL debugLoggingEnabled;
@property (nonatomic) BOOL customUserLocationAnnnotationEnabled;

@property (nonatomic) BOOL isTestingFeaturesInBox;
@property (nonatomic) UIView *featuresInBoxView;
@property (nonatomic) CLLocationCoordinate2D countPointCoordinate;
@property (nonatomic) UILabel *countLabel;

@end

@implementation MBXViewController
Expand Down Expand Up @@ -289,6 +295,7 @@ - (void)dismissSettings:(__unused id)sender
@"Add 10,000 Sprites",
@"Add Test Shapes",
@"Add Point With Custom Callout",
@"Query Annotations",
@"Remove Annotations",
]];
break;
Expand Down Expand Up @@ -387,6 +394,9 @@ - (void)performActionForSettingAtIndexPath:(NSIndexPath *)indexPath
case MBXSettingsAnnotationsCustomCallout:
[self addAnnotationWithCustomCallout];
break;
case MBXSettingsAnnotationsQueryAnnotations:
[self testQueryPointAnnotations];
break;
case MBXSettingsAnnotationsRemoveAnnotations:
[self.mapView removeAnnotations:self.mapView.annotations];
break;
Expand Down Expand Up @@ -875,6 +885,20 @@ - (void)toggleCustomUserDot
self.mapView.userTrackingMode = MGLUserTrackingModeFollow;
}

- (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];
}

- (void)printTelemetryLogFile
{
NSString *fileContents = [NSString stringWithContentsOfFile:[self telemetryDebugLogFilePath] encoding:NSUTF8StringEncoding error:nil];
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 @@ -892,6 +892,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 @@ -2796,6 +2796,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 861c066

Please sign in to comment.