Creating custom annotations

If you have ever owned an iPhone and used Apple Maps, you will be familiar with pins. When you have a map inside of your app, having custom pins (annotations) gives your app a bit more polish. Let's create our custom annotations.

Open up MapViewController in the Navigator Panel, then inside of the initialize() method, add the following:

mapView.delegate = self

Next, add the following directly under the addMap(_ annotations:) method:

func mapView(_ mapView:MKMapView, viewFor annotation:MKAnnotation) -> MKAnnotationView? {    let identifier = "custompin"    guard !annotation.isKind(of: MKUserLocation.self) else { return nil }    var annotationView: MKAnnotationView? if let customAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: ...

Get iOS 12 Programming for Beginners - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.