user1904273
user1904273

Reputation: 4764

Add full blown annotation view with driving directions using Mapkit and Swift

I am using the the following code to create an annotation pin on a map. When selected, it shows the name of the MKMapItem such as McDonald's. However, I would like it to show a full blown annotation with more detail including address, picture if available and driving directions. The tutorials I've seen seen on Ray Wenderlich and elsewhere go further than I need in terms of introducing custom artwork, a custom class and so forth. I just the basics and a button for driving directions.

How would I expand the following to show more detail and a button for driving directions?

mapView.mapType = MKMapType.standard
let location : CLLocationCoordinate2D = self.aRestaurant.placemark.coordinate

let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = self.aRestaurant.placemark.name
mapView.addAnnotation(annotation)

Thanks in advance for any suggestions.

Upvotes: 0

Views: 154

Answers (1)

Gerd Castan
Gerd Castan

Reputation: 6849

in your MKAnnotationView set the property

self.detailCalloutAccessoryView = calloutView

calloutView can be any subclass of UIView with subViews including UIButtons for some user interaction.

calloutView replaces the subtitle. The title will still be visible.

For simple cases you can add

leftCalloutAccessoryView = myButton1
rightCalloutAccessoryView = myButton2

For buttons that are displayed on the left and right side of the center view.

If you use MKMarkerAnnotationViews, you also get the title and subtitle for free below the markerView on the map, so that the user sees the title + subtitle without opening the AnnotationView.

Upvotes: 1

Related Questions