2

I've been trying to call a function when a pin on my map is clicked. I have about ten pins on my map, so how can I determine which pin is pressed and have all the data that the MKPointAnnotation contains?

How each annotation is added to the map:

 let map = MKMapView(frame: .zero)
 let annotation = MKPointAnnotation()

 annotation.coordinate = donator.coordinates
 annotation.title = donator.name
 annotation.subtitle = donator.car
 map.addAnnotation(annotation)

Thanks!

Tucker
  • 57
  • 7
  • You could add a tap gesture recognizer to `MKAnnotationView` – Mihir Luthra Nov 22 '20 at 05:17
  • SwiftUI iOS 14 MapKit Annotation with Tap Gesture link at apple developers forum - https://developer.apple.com/forums/thread/659748?answerId=655389022#655389022 – SHS Jan 18 '21 at 13:30

1 Answers1

1

Assuming that you're wrapping your MKMapView inside a UIViewRepresentable struct, add a coordinator with the MKMapViewDelegate protocol to listen for changes on your map:

//Inside your UIViewRepresentable struct
func makeCoordinator() -> Coordinator {
    Coordinator()
}

class Coordinator: NSObject, MKMapViewDelegate {
    //Delegate function to listen for annotation selection on your map
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        if let annotation = view.annotation {
            //Process your annotation here
        }
    }
}

There are a couple of tutorials out there on how to include an MKMapView in SwiftUI and use delegation to access the MKMapViewDelegate functions through UIViewRepresentable and coordinators.

Following along my suggestion, your previous code would look like so:

struct MapKitView: UIViewRepresentable {

    typealias Context = UIViewRepresentableContext<MapKitView>

    func makeUIView(context: Context) -> MKMapView {
        let map = MKMapView()
        map.delegate = context.coordinator
        let annotation = MKPointAnnotation()

        annotation.coordinate = donator.coordinates
        annotation.title = donator.name
        annotation.subtitle = donator.car
        map.addAnnotation(annotation)
        return map
    }

    //Coordinator code
    func makeCoordinator() -> Coordinator { ... }
}
Raphael
  • 68
  • 7