Hello,
I've implemented directions to my annotations on map. But when Apple Maps are opening I can only see Uknown Location as a destination point. What I would like to do is to have Annotation title instead of Unknown Location. Here is my code:
class AnnotationButton: UIButton {
var annotation: MKPointAnnotation?
}
func findPlace(_ places: [Place]) {
for place in places {
let annotations = MKPointAnnotation()
annotations.title = place.name
annotations.subtitle = place.description
annotations.coordinate = CLLocationCoordinate2D(latitude:
place.lattitude, longitude: place.longtitude)
mapView.addAnnotation(annotations)
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else { return nil }
let identifier = "identifier"
guard let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation) as? MKMarkerAnnotationView else { return nil }
let leftButton = AnnotationButton(frame: CGRect(
origin: CGPoint.zero,
size: CGSize(width: 25, height: 25)))
leftButton.setBackgroundImage(#imageLiteral(resourceName: "nav"), for: .normal)
annotationView.leftCalloutAccessoryView = leftButton
leftButton.addTarget(self, action: #selector(didClickDetailDisclosureNavigation(button:)), for: .touchUpInside)
if let pointAnnotation = annotation as? MKPointAnnotation {
leftButton.annotation = pointAnnotation
}
@objc func didClickDetailDisclosureNavigation(button: AnnotationButton) {
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
if let mapItem = button.annotation?.mapItem {
mapItem.openInMaps(launchOptions: launchOptions)
}
}
extension MKPointAnnotation {
var mapItem: MKMapItem {
let placemark = MKPlacemark(coordinate: self.coordinate)
return MKMapItem(placemark: placemark)
}
}