Swift MapKit Annotation

Swift MapKit Annotation

Created By : Debasis Das (31-July-2016)

We will be working in a series of Sample Code and Proof of concepts to play around MapKit.framework.

This article is the first in the series to demonstrate swift mapkit annotation

Here we will load a MapView from a Plist files which contains the latitude and longitude of different places/cities in India.

We will use the simple pin annotations in the sample code


The output of the program is as given below

Swift Mapkit Annotation 1

Swift Mapkit Annotation 1

Swift Mapkit Annotation 2

Swift Mapkit Annotation 2

Sample Code

//  AppDelegate.swift
//  MapSampleOne
//  Created by Debasis Das on 28/07/16.
//  Copyright © 2016 Knowstack.com. All rights reserved.

import Cocoa
import MapKit

class AppDelegate: NSObject, NSApplicationDelegate, MKMapViewDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var mapView:MKMapView!
    var mapAnnotations:[PinAnnotation] = []

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        self.mapView.delegate = self
        //Setting the region to a state coordinate
        var newRegion:MKCoordinateRegion = MKCoordinateRegion()
        newRegion.center.latitude = 20.2961;
        newRegion.center.longitude = 85.8245;
        newRegion.span.latitudeDelta = 20.112872;
        newRegion.span.longitudeDelta = 20.109863;
        self.mapView.setRegion(newRegion, animated: true)
        //Reading cities and its latitude and longitude from a plist file
        let cityDetailsPath = NSBundle.mainBundle().pathForResource("IndianCitiesSample", ofType: "plist")
        let cityDetails = NSArray(contentsOfFile: cityDetailsPath!) as? [[String:String]]
        for city in cityDetails!{
            let lat = Double(city["latitude"]!)
            let lon = Double(city["longitude"]!)
            let title = city["title"]
            let subTitle = city["subTitle"]
            let annotation:PinAnnotation = PinAnnotation(coordinate: CLLocationCoordinate2DMake(lat!, lon!), title: title!, subtitle: subTitle!)



    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
    func mapView(mapView: MKMapView, didFailToLocateUserWithError error: NSError) {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let returnedAnnotationView:MKAnnotationView = PinAnnotation.createViewAnnotationForMap(self.mapView, annotation: annotation)
        let button = NSButton(frame: NSMakeRect(0,0,100,80))
        button.title = "More Info"
        button.target = self
        button.action = Selector("showMoreInfo:")
        button.bezelStyle = .RegularSquareBezelStyle
        returnedAnnotationView.rightCalloutAccessoryView = button
        return returnedAnnotationView

    @IBAction func showMoreInfo(sender:AnyObject){
        print("show popover to show more info")
        let alert = NSAlert()
        alert.messageText = "show details"
        alert.informativeText = "Informative text goes here"
        alert.beginSheetModalForWindow(self.window, completionHandler: nil)


//PinAnnotation class that implements the MKAnnotation protocol
class PinAnnotation:NSObject, MKAnnotation{
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    class func createViewAnnotationForMap(mapView:MKMapView, annotation:MKAnnotation)->MKAnnotationView{
        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("PinAnnotation"){
                return annotationView
            let returnedAnnotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier:"PinAnnotation")
            returnedAnnotationView.pinTintColor = NSColor.redColor()
            returnedAnnotationView.animatesDrop = true
            returnedAnnotationView.canShowCallout = true
            return returnedAnnotationView

You can download the sample code Swift MapKit Annotation – Sample One

Posted in MapKit, Swift Tagged with: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *