Is there a way to know if the MKAnnotationView was selected by the user or programmatically?

Viewed 29

I have a map with annotations, but when I select it programmatically, the map should do one logic on centering. When the user selects it, another logic must be done to center.

Is there a way to know when it is selected programmatically or by the user?

I'm using the following method of the mapView delegate:

public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
1 Answers

One solution to this would be to create the MKAnnocationPoint yourself and to do something about it in didselect. As you said "When the user selects it, another logic must be done to center." . You can control it like

class MyAnnotation : MKPointAnnotation {
    ... // you can declare some property to manage annocation
}

var selectedAnnotation: MyAnnotation?

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
   self.selectedAnnotation = view.annotation as? MyAnnotation
   if selectedAnnotation != nil{ 
      // do what you want
   }

}

If you do this process , you must add this annocation (MyAnnotation) in mapview's viewFor

Related