Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we get all the annotation views from MKMapView

Is there any direct method or any suggestion on how to get all the annotations from the MKMapView?

like image 273
Madan Mohan Avatar asked Jan 17 '11 08:01

Madan Mohan


People also ask

How do you get zoom level in MKMapView?

The easiest way to get an Integer of the current zoom level, is by using the MapView function: regionDidChangeAnimated. This function recognizes every change in zoom and will give you the basis for the calculation of the zoom factor.

What is MKMapView?

The MKMapView class supports the ability to annotate the map with custom information. Because a map may have large numbers of annotations, map views differentiate between the annotation objects used to manage the annotation data and the view objects for presenting that data on the map.

How do I use MapKit in swift 5?

Go to the storyboard. Drag a MapKit View to the main View. Give the MapKit View the same size as the main View. Select the MapKit View and go to the Pin button from the Auto Layout button on the bottom-right of the Storyboard and fill in the following values.


2 Answers

You can access map's annotations using its annotations property. Getting views for all annotations may not be always possible as annotations that are not currently visible on the map may have no views associated with them, but for arbitrary annotation you can try to get a view using -viewForAnnotation: method.

So here how you can iterate through all map's annotations and try to access their views:

for (id<MKAnnotation> annotation in mapView.annotations){     MKAnnotationView* anView = [mapView viewForAnnotation: annotation];     if (anView){        // Process annotation view        ...     } } 
like image 90
Vladimir Avatar answered Sep 27 '22 16:09

Vladimir


In swift 3.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {      let selectedAnnotation = view.annotation          for annotation in mapView.annotations {             let viewI = mapView.view(for: annotation)              if !(viewI?.annotation is MKUserLocation){                 if annotation.isEqual(selectedAnnotation) {                     viewI?.image = UIImage(named: "focus.png")                     showLifeEventView(anotation: view) //did select a point on Map.                 }else{                     viewI?.image = UIImage(named: "point.png")                 }             }         } } 
like image 20
Cristian Cardoso Avatar answered Sep 27 '22 17:09

Cristian Cardoso