Adding An Annotation Or Pin To Your MapView

In this article, you will learn about Map Annotation in iOS.

Introduction

In this article, we will look forward and implement the Map Kit Annotation. If you know Android app development, you have additional points to implement in iOS, but if you have no past experience, don’t worry. It’s not too much harder  to implement. Keep focused on my steps and you will easily do this project.

Step 1

Now, open the Xcode IDE, create a Single View Application and chose the universal type instead of iPad and iPhone. Choose the language Swift and give the name of the project -- it depends upon you --  and create a project. Afterwards, go to the Main Storyboard file and you will see the empty ViewController. Drag the Map Kit from the object library and drop into the ViewController.

ViewController

Step 2

Open the ViewController.Swift file and first import the Map Kit library at the top. Open the Xcode editor in the assistant mode and make the connection between the Main Story Board file and ViewController.Swift file. Afterwards, you will see in the top position, that I added the Map Kit Library.

Map Kit Library

Step 3

Now, for annotation in map kit, we give the longitude and latitude position and then we will set the zoom degree. When map is open, how much is zoom appearing near to the position? Then we create a constant variable called location and add the CLLocationCoordinate2D with the latitude and the longitude. We repeat the same step for MKCoordinateSpanMake, add the latDelta, LonDelta , we add into the mapView and set the animationtrue. We create the annotation constant variable with MKPointAnnotation and create its instance. Subsequently, we add the title and subtitle and all these things in annotation instance, as shown below:

  1. import UIKit  
  2. import MapKit  
  3. class ViewController: UIViewController   
  4. {  
  5.     @IBOutlet weak  
  6.     var mapView: MKMapView!override func viewDidLoad()   
  7.     {  
  8.         super.viewDidLoad()  
  9.             // 41.036803, 28.986542  
  10.         let latitude: CLLocationDegrees = 41.036803  
  11.         let longitude: CLLocationDegrees = 28.986542  
  12.         let latDelta: CLLocationDegrees = 0.01  
  13.         let lonDelta: CLLocationDegrees = 0.01  
  14.         let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)  
  15.         let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)  
  16.         let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)  
  17.         self.mapView.setRegion(region, animated: true)  
  18.         let annotation: MKPointAnnotation = MKPointAnnotation()  
  19.         annotation.coordinate = location  
  20.         annotation.title = "India"  
  21.         annotation.subtitle = "C-sharpcorner.com"  
  22.         self.mapView.addAnnotation(annotation)  
  23.     }  
  24.     override func didReceiveMemoryWarning()   
  25.     {  
  26.         super.didReceiveMemoryWarning()  
  27.             // Dispose of any resources that can be recreated.  
  28.     }  
  29. }  
Step 4

Now, run the app. You will see the pointer is here, which tells the location according to the longitude and latitude position. In my app, you will see that only pointer appears and map doesn’t appear because of the internet slow speed issue and I apologize for this.

app