How to count number and which annotations shown on MapView, when zooming?
Asked Answered
J

3

9

I need to count number of annotations when i zooming mapView, and get array with which are shown on map, and then, i need to reload my Table and show list of only which shown on map.

How can i get number and array with annotation?

Jaal answered 13/8, 2013 at 7:26 Comment(0)
H
10

How about these two methods from MapKit class:

1) Get visible rectangle of map using:

@property(nonatomic, readonly) CGRect annotationVisibleRect

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/annotationVisibleRect

2) Then get annotation NSSet from parameter map rectangle:

- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/annotationsInMapRect:

So I would expect something like this:

-(void)getAnotationsInVisibleMapRectangle
{
    NSSet *annotationSet = [myMapView annotationsInMapRect:myMapView.annotationVisibleRect];

    // print number of annotations
    NSLog(@"Number of annotations in rect: %d", annotationSet.count);

    // this will return an array from the NSSet
    NSArray *annotationArray = [annotationSet allObjects]; 
}

Does that help?

Homopterous answered 13/8, 2013 at 7:47 Comment(3)
It's nice, but need to convert CGRect (myMapView.annotationVisibleRect) to MKMapRect? "annotationInMapRect(MKMapRect)"...Jaal
Thanks! =) It help me) and i use "visibleMapRect" instead of "annotationVisibleRect"Jaal
'annotationVisibleRect' does not compile so use 'visibleMapRect' as @Jaal noted.Illusive
C
12

As for swift 2.0, I usually use this mapView extension to get all currently visible annotations as an array of MKAnnotations:

extension MKMapView {
    func visibleAnnotations() -> [MKAnnotation] {
        return self.annotationsInMapRect(self.visibleMapRect).map { obj -> MKAnnotation in return obj as! MKAnnotation }
    }
}
Checkmate answered 23/9, 2015 at 1:1 Comment(2)
if some annotations are at the edge of the screen, it doesn't workIroning
For that particular scenario, you could pass a bigger MapRect instead of the default visibleMapRect.Checkmate
H
10

How about these two methods from MapKit class:

1) Get visible rectangle of map using:

@property(nonatomic, readonly) CGRect annotationVisibleRect

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/annotationVisibleRect

2) Then get annotation NSSet from parameter map rectangle:

- (NSSet *)annotationsInMapRect:(MKMapRect)mapRect

http://developer.apple.com/library/ios/DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instm/MKMapView/annotationsInMapRect:

So I would expect something like this:

-(void)getAnotationsInVisibleMapRectangle
{
    NSSet *annotationSet = [myMapView annotationsInMapRect:myMapView.annotationVisibleRect];

    // print number of annotations
    NSLog(@"Number of annotations in rect: %d", annotationSet.count);

    // this will return an array from the NSSet
    NSArray *annotationArray = [annotationSet allObjects]; 
}

Does that help?

Homopterous answered 13/8, 2013 at 7:47 Comment(3)
It's nice, but need to convert CGRect (myMapView.annotationVisibleRect) to MKMapRect? "annotationInMapRect(MKMapRect)"...Jaal
Thanks! =) It help me) and i use "visibleMapRect" instead of "annotationVisibleRect"Jaal
'annotationVisibleRect' does not compile so use 'visibleMapRect' as @Jaal noted.Illusive
M
4

David's great solution in Swift 4

extension MKMapView {
    func visibleAnnotations() -> [MKAnnotation] {
        return self.annotations(in: self.visibleMapRect).map { obj -> MKAnnotation in return obj as! MKAnnotation }
    }
}
Motheaten answered 25/4, 2018 at 8:19 Comment(1)
All of the suggestions above don't work well if map is rotated.Groundsheet

© 2022 - 2024 — McMap. All rights reserved.