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.
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.
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:
- import UIKit
- import MapKit
- class ViewController: UIViewController
- {
- @IBOutlet weak
- var mapView: MKMapView!override func viewDidLoad()
- {
- super.viewDidLoad()
-
- let latitude: CLLocationDegrees = 41.036803
- let longitude: CLLocationDegrees = 28.986542
- let latDelta: CLLocationDegrees = 0.01
- let lonDelta: CLLocationDegrees = 0.01
- let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
- let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
- let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
- self.mapView.setRegion(region, animated: true)
- let annotation: MKPointAnnotation = MKPointAnnotation()
- annotation.coordinate = location
- annotation.title = "India"
- annotation.subtitle = "C-sharpcorner.com"
- self.mapView.addAnnotation(annotation)
- }
- override func didReceiveMemoryWarning()
- {
- super.didReceiveMemoryWarning()
-
- }
- }
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.