This post shows you how to add annotations (aka pins) to a map using Swift and the Map Kit framework.
Specifically, this post uses the MKPointAnnotation class to create a basic annotation as mentioned in Apple’s Location and Maps Programming Guide. Note, alternatively you could create an annotation object by defining a custom object that conforms to the MKAnnotation protocol, also mentioned in Apple’s Location and Maps Programming Guide.
Here is a picture of the annotation on the map:
As shown in the next image, the annotation object used in this example also has a callout bubble that is shown if the user clicks on the annotation.
Here are two other resources I found on this topic:
1) http://www.ioscreator.com/tutorials/mapkit-tutorial-swift-ios8 (This one I used to learn the basics and my code reflects this.)
2) http://www.youtube.com/watch?v=uB100xVS_Yc
Instructions:
- Open XCode
- File->New->Project
- In iOS Application section select Single View Application and click Next.
- Name & save as you desire.
- In the Project Navigator, open the Main.storyboard file.
- In the File Inspector of the Utilities pane, uncheck “Use Size Classes”. (This is not really part of understanding the Annotations, but just to tell XCode we want to design for a specific device. In this case, the iPhone.)
- Find MapKit View in the Object Library and drag 1 onto the view.
- Set the MKMapView size using the dashed blue lines of XCode’s AutoLayout.
- Use the Document Outline area to select the Map View.
- Select Editor->Resolve Auto Layout Issues->Reset to Suggested Constraints.
- In the Project Navigator, open the ViewController.swift file.
- import MapKit
- Now use the Assistant Editor to create an outlet by dragging from the MKMapview in the Main.storyboard file to just below the class definition (i.e. not inside one of the functions) in the ViewController.swift file.
- In the viewDidLoad method below the call to the super’s viewDidLoad method:
- Create a constant to store a CLLocationCoordinate2D structure.
- let sfGiantsStadiumLocation = CLLocationCoordinate2D(latitude: 37.77831, longitude: -122.38758)
- Create a variable to store an MKCoordinateSpan structure.
- var span = MKCoordinateSpanMake(0.01, 0.01)
- Create a variable to store an MKCoordianteRegion structure
- var coordinateRegion = MKCoordinateRegion(center: sfGiantsStadiumLocation, span: span)
- Set the region of the MKMapView object.
- map.setRegion(coordinateRegion, animated: true)
- Create an MKPointAnnotation object.
- let sfGiantsStadiumAnnotation = MKPointAnnotation()
- Set properties of the MKPointAnnotation object.
- (see code below)
- Add the MKPointAnnotation object to the MKMapView object.
- map.addAnnotation(sfGiantsStadiumAnnotation)
- Create a constant to store a CLLocationCoordinate2D structure.
All the code for this example goes in the ViewController.swift file. Here is the code:
import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//set lat/lon coordinates of a location
let sfGiantsStadiumLocation = CLLocationCoordinate2D(latitude: 37.77831, longitude: –122.38758)
//set a span to be used by the MKCoordinateRegion structure
var span = MKCoordinateSpanMake(0.01, 0.01)
//create MKCoordinateRegion structure
var coordinateRegion = MKCoordinateRegion(center: sfGiantsStadiumLocation, span: span)
//set the region of the MKMapView object
map.setRegion(coordinateRegion, animated: true)
//create an MKPointAnnotation object
let sfGiantsStadiumAnnotation = MKPointAnnotation()
//set properties of the MKPointAnnotation object
sfGiantsStadiumAnnotation.setCoordinate(sfGiantsStadiumLocation)
sfGiantsStadiumAnnotation.title = “AT & T Park”
sfGiantsStadiumAnnotation.subtitle = “Home of the San Francisco Giants”
//add the annotation to the map
map.addAnnotation(sfGiantsStadiumAnnotation)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}