NEW: Learn SwiftUI with my free YouTube video series! >>

How to add annotations to MKMapView using MKPointAnnotation and MKPinAnnotationView

Swift version: 5.0

Paul Hudson    @twostraws   

Once you have an MKMapView up and running, it only takes a few lines of code more to drop pins containing placemarks.

Start by making your view controller the delegate of your map view, so that we can receive events. You should also make your view controller conform to MKMapViewDelegate in code.

Adding pins to the map takes two code changes. First you need to create an annotation describing where the pin is and what its name is – put this in your viewDidLoad() method:

let london = MKPointAnnotation()
london.title = "London"
london.coordinate = CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275)
yourMapView.addAnnotation(london)

Second, you need to implement a viewFor method that converts your annotation into a view that can be displayed on the map. iOS comes with a built-in view type called MKPinAnnotationView that provides the familiar pin layout, so we can use that here.

Note: For performance reasons, dropping pins onto a map works using re-use identifiers, just like loading table view cells. The code below tries to re-use pins, and you should do the same.

Add this to your view controller:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard annotation is MKPointAnnotation else { return nil }

    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }

    return annotationView
}

That’s all the code you need!

SPONSOR Meet the new Instabug – more than just bug reporting! We help you build better apps and minimize your debugging time. With each bug report, we automatically capture details like network requests, repro steps, and session details. Get real-time crash reports with stack trace details and session data to help you catch and fix issues easily. And with our customizable in-app surveys, you’ll gather insightful user feedback and much more. Instabug is the fastest and easiest way to release with confidence. Start your free trial now! Start your free trial now!

Available from iOS 4.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 4.2/5

Click here to visit the Hacking with Swift store >>