I am fairly new to MapKit and am attempting to show information and directions once the place marker is selected. I am displaying local hospitals and emergency services. How do I get the information of the currently selected placemarker. I want to be able to show a few lines of information about the selected marker. Such as name, address, phone number, and possibly a button for directions. I want to save the currently selected markers coordinates into a variable.
class MapKitViewController: UIViewController, MKMapViewDelegate {
let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
var previousLocation: CLLocation?
let geoCoder = CLGeocoder()
var directionsArray: [MKDirections] = []
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
performSearch()
checkLocationAuthorization()
} else {
// Show alert letting the user know they have to turn this on.
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
-> MKAnnotationView? {
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(
withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view =
MKMarkerAnnotationView(annotation: annotation,
reuseIdentifier: identifier)
view.markerTintColor = UIColor.blue
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
func mapView(_: MKMapView, annotationView:
MKAnnotationView, calloutAccessoryControlTapped: UIControl) {
print("Control tapped")
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
}