How To Add Map Annotations In iOS (Swift 3 + Xcode 8) ?

How To Add Map Annotations In iOS

Hi, In this tutorial, I’ll show you how to place an annotation on map in your iOS apps. Its super easy and you can learn to add annotations in map just by reading this small post only once. So, let’s get started and see how to do it.

First of all, open your Xcode and create a Single View Application. Now, give some name to your project and make sure to choose Swift as the language. 

Now, drag and drop a Map View from objects library to your storyboard’s view controller. Now, place some constraints on this map view. For that, just click on Add constraint’s icon on right bottom corner of your screen and add 0 from all sides so that map can be visible on whole app screen. You can check below image for your reference:



After adding constraints, ctrl+drag this map view to your ViewController.swift file and create its outlet.

Now, open your ViewController.swift file. Here, first import MapKit framework.

import MapKit

Now, paste following code in your viewDidLoad() method:

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let location = CLLocationCoordinate2DMake(30.7333,76.7794)        //1
        
        let span = MKCoordinateSpanMake(0.1,0.1)        //2
        
        let region = MKCoordinateRegionMake(location, span)     //3
        
        Map.setRegion(region, animated: true)       //4
        
        let annotation = MKPointAnnotation()        //5
        annotation.coordinate = location        //6
        annotation.title = "Chandigarh"       //7
        annotation.subtitle = "Its my city."      //8
        
        Map.addAnnotation(annotation)       //9
        
    }

Here, 
  1. First, we’ll define our location's coordinates here.
  2. We will create a span to show our place within some specific zoomed area. You can set and check it with any other value for setting up zoom on your place.
  3. Here, we’re creating a region on our map to show our location and span.
  4. Here, we’re setting our created region on our Map.
  5. Here, we’re creating our annotation object.
  6. Here, we’re passing our location coordinate to this annotation.
  7. Here’ we’re giving our annotation a title so that it pop up whenever user clicks on this annotation.
  8. We’re giving a subtitle to our annotation.
  9. Just adding this annotation on our map view.

Now, run your app and you’ll see your saved annotation on the map of your iOS app and when you click on it, it will populate its title and sub title. It will look like this below image:



Looking nice and cool, right?? 😊

So, that’s all for this tutorial. I hope this post may help someone, somehow!

If you've any doubts or need any further help, then do message me in comments section below and I'll try to respond you as soon as possible.


Thanks for your time. Keep Coding...

Comments

Popular posts from this blog

How To Make The Device Vibrate Or Ring- An iOS Swift Tutorial

PayPal Integration In iOS Applications | Payment Gateways iOS/Swift

How To Parse JSON Data In iOS (Swift 3 + Xcode 8) ?