How to call a function or tell if a MKPointAnnotation is clicked on a MKMapView SwiftUI

531 Views Asked by At

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!

1

There are 1 best solutions below

0
Raphael On BEST ANSWER

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 { ... }
}