I am trying to perform a certain function whenever an MKPlacemark is clicked on my MapView. This function pulls information from the placemark and displays it on a separate view. I have tried to use
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {}
but this doesn't seem to work. I'm not sure if I have to name each placemark, I have avoided doing so because I am already displaying location data in the name (as can be seen in my code). Any help would be much appreciated. My code:
Initiating the placemark (this happens numerous times, the variables have already been instantiated):
let coords = CLLocationCoordinate2DMake(latOfPlace!, lonOfPlace!)
let address = [CNPostalAddressStreetKey: addressStreetKey, CNPostalAddressCityKey: addressCityKey, CNPostalAddressPostalCodeKey: addressPostalCodeKey, CNPostalAddressISOCountryCodeKey: addressISOCountryCodeKey]
let place = MKPlacemark(coordinate: coords, addressDictionary: address)
self.mapView.addAnnotation(place)
You need to set the delegate inside viewDidLoad
self.mapView.delegate = self
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let ann = view.annotation as? MKPlacemark else { return }
print(ann)
}