Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 357
Nubaslon Avatar asked Aug 13 '13 07:08

Nubaslon


2 Answers

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 }
    }
}
like image 168
Jordi Bruin Avatar answered Nov 03 '22 21:11

Jordi Bruin


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 }
    }
}
like image 31
David H. Avatar answered Nov 03 '22 23:11

David H.