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!
Assuming that you're wrapping your
MKMapViewinside aUIViewRepresentablestruct, add a coordinator with theMKMapViewDelegateprotocol to listen for changes on your map:There are a couple of tutorials out there on how to include an MKMapView in SwiftUI and use delegation to access the
MKMapViewDelegatefunctions throughUIViewRepresentableand coordinators.Following along my suggestion, your previous code would look like so: