My demo application is for showing how to use annotations on the map view using Swift 3 (and Xcode 8). It's very simple. When you launch the app, it will show a map and the button "My Annotations". When you tap on the button, the map will navigate to your birth place. Tap on it again, it will show my favorite place called "RUPP". The image below shows how the app looks like when your launch it.
Related classes:
- MKMapView: the map view to be shown on the screen
- MKAnnotation: the info about your location such as coordinate and title
- MKAnnotationView: use it to define how the annotation (your location) looks on the map
1. Create a single application in Xcode
Suppose you already created the application in Xcode. There should be one View Controller in Interface Builder by default.
2. Update ViewController class as following:
import UIKit
import MapKit
class MapViewAnnotationDemoController: UIViewController, MKMapViewDelegate {
var mapView: MKMapView!
var myAnnotationsButton: UIButton!
private var myAnnotations = [MKAnnotation]()
private var currentAnnotationIndex: Int = 0
override func loadView() {
mapView = MKMapView()
mapView.delegate = self
view = mapView
}
override func viewDidLoad() {
initMyAnnotationsButton()
initMyAnnotations()
}
private func initMyAnnotationsButton() {
myAnnotationsButton = UIButton(frame: CGRect(x: 8, y:40, width: 140, height: 20))
myAnnotationsButton.setTitle("My Annotations", for: UIControlState.normal)
myAnnotationsButton.backgroundColor = UIColor.green
myAnnotationsButton.addTarget(self, action: #selector(showMyAnnotations(_:)), for: UIControlEvents.touchUpInside)
view.addSubview(myAnnotationsButton)
}
private func initMyAnnotations() {
let myBirthPlaceAnnotation = MKPointAnnotation()
myBirthPlaceAnnotation.title = "My Birth Place (title)"
myBirthPlaceAnnotation.subtitle = "My Birth Place (subtitle)"
myBirthPlaceAnnotation.coordinate.latitude = 11.5564
myBirthPlaceAnnotation.coordinate.longitude = 104.9282
myAnnotations.append(myBirthPlaceAnnotation)
let ruppAnnotation = MKPointAnnotation()
ruppAnnotation.title = "RUPP (title)"
ruppAnnotation.subtitle = "RUPP (subtitle)"
ruppAnnotation.coordinate.latitude = 11.5690
ruppAnnotation.coordinate.longitude = 104.8907
myAnnotations.append(ruppAnnotation)
mapView.addAnnotations(myAnnotations)
}
func showMyAnnotations(_ sender: UIButton) {
print("\nshowMyAnnotations() called")
if currentAnnotationIndex == myAnnotations.count - 1 {
currentAnnotationIndex = 0
} else {
currentAnnotationIndex += 1
}
mapView.showAnnotations([myAnnotations[currentAnnotationIndex]], animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print("\nmapView(viewFor) called")
let pinAnnotationView = MKPinAnnotationView()
pinAnnotationView.annotation = annotation
pinAnnotationView.animatesDrop = true
return pinAnnotationView
}
}
In
initMyAnnotations() method, we called
mapView.addAnnotations(myAnnotations) method. And the
addAnnotations() method calls the
mapView(_:MKMapView, viewFor: MKAnnotation) method of the map view's delegate to get the instance of
MKAnnotationView so the map view will know how to display the annotation. The delegate is the
ViewController class in this case cause it extends
MKMapViewDelegate and we set
mapView.delegate to
self.