diff options
Diffstat (limited to 'platform/macos/app/MapDocument.m')
-rw-r--r-- | platform/macos/app/MapDocument.m | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 03557caca2..23bc652229 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -641,6 +641,51 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio repeats:YES]; } + +- (id<MGLAnnotation>)randomOffscreenPointAnnotation { + + NSPredicate *pointAnnotationPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { + return [evaluatedObject isKindOfClass:[MGLPointAnnotation class]]; + }]; + + NSArray *annotations = [self.mapView.annotations filteredArrayUsingPredicate:pointAnnotationPredicate]; + + if (annotations.count == 0) { + return nil; + } + + // NOTE: self.mapView.visibleAnnotations occasionally returns nil - see + // https://github.com/mapbox/mapbox-gl-native/issues/11296 + NSArray *visibleAnnotations = [self.mapView.visibleAnnotations filteredArrayUsingPredicate:pointAnnotationPredicate]; + + NSLog(@"Number of visible point annotations = %ld", visibleAnnotations.count); + + if (visibleAnnotations.count == annotations.count) { + return nil; + } + + NSMutableArray *invisibleAnnotations = [annotations mutableCopy]; + + if (visibleAnnotations.count > 0) { + [invisibleAnnotations removeObjectsInArray:visibleAnnotations]; + } + + // Now pick a random offscreen annotation. + uint32_t index = arc4random_uniform((uint32_t)invisibleAnnotations.count); + return invisibleAnnotations[index]; +} + +- (IBAction)selectOffscreenPointAnnotation:(id)sender { + id<MGLAnnotation> annotation = [self randomOffscreenPointAnnotation]; + if (annotation) { + [self.mapView selectAnnotation:annotation]; + + // Alternative method to select the annotation. These two should do the same thing. + // self.mapView.selectedAnnotations = @[annotation]; + NSAssert(self.mapView.selectedAnnotations.firstObject, @"The annotation was not selected"); + } +} + - (void)updateAnimatedAnnotation:(NSTimer *)timer { DroppedPinAnnotation *annotation = timer.userInfo; double angle = timer.fireDate.timeIntervalSinceReferenceDate; @@ -1111,6 +1156,9 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio if (menuItem.action == @selector(insertGraticuleLayer:)) { return ![self.mapView.style sourceWithIdentifier:@"graticule"]; } + if (menuItem.action == @selector(selectOffscreenPointAnnotation:)) { + return YES; + } if (menuItem.action == @selector(showAllAnnotations:) || menuItem.action == @selector(removeAllAnnotations:)) { return self.mapView.annotations.count > 0; } |