How can I detect which annotation was selected in MapView

前端 未结 2 786
名媛妹妹
名媛妹妹 2021-02-14 09:52

I\'ve made a few annotations inside the Map and when I tap on them I see some informations and I have a button that opens Maps and with the right information that I can\'t take

相关标签:
2条回答
  • 2021-02-14 10:22

    For Swift 4

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        var selectedAnnotation = view.annotation
    }
    
    0 讨论(0)
  • 2021-02-14 10:35

    For that you can use selected Annotation from didSelectAnnotationView, then store that annotation to instance variable and after that used annotation in your Button action method.

    var selectedAnnotation: MKPointAnnotation?
    
    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        self.selectedAnnotation = view.annotation as? MKPointAnnotation
    }
    
    func info(sender: UIButton) {
        print(selectedAnnotation?.coordinate)
    }
    

    Edit: As of you have custom MKAnnotation class you need to use that.

    var selectedAnnotation: Islands?
    
    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        self.selectedAnnotation = view.annotation as? Islands
    }    
    
    0 讨论(0)
提交回复
热议问题