Get user current location in swift

Get user location in swift using CLLocationManager

Introduction to the post get user location in swift4 using CLLocationManager

In this tutorial, we will learn how to get user current location in swift. For getting user location, we will use Corelocation framework for location updates and Mapkit framework to show user current position on map. We wil use apple default map not Google map.

Steps to get user location in swift

 
Step 1: Create a new project using “Single view app” template and name it “UserLocationAndMapkit”.
 
Step 2: Open “Main.storyboard” and on your ViewController’s view and drag “Map Kit View” i.e. MKMapView.
 
Get User  location in swift4 - tutorial
 
 
Step 3: Open “ViewController.swift” and create IBOutlet for “MKMapView”. Also import “MapKit” framework.
 

 
 
Step 4: Open “Main.storyboard” and connect mapView IBOutlet to the map aka MKMapView placed on ViewController’s view.
 
Connecting IBOutlet
 
 
Step 5:  Open “ViewController.swift”. First import CoreLocation framework so that we can use classes to get location of the device. Below is the code that we used to get location of the user.
 
 

 
In the above code, we created instance of CLLocationManager and named it locationManager. Inside viewDidLoad function, first we will set MKMapView, “showsUserLocation” property to true. This will display blue dot indicating current location for the user on mapView. Then we detect if location services are enabled for the device on which our app is running, if not then control will move’s to else block where we print a message, though you can show alert to user for better user experience.
 

Requesting access from user for getting his location

 
                                           If locationServicesEnabled return true then we detect if user authorized our app to use location services or not, If authorization status comes as “denied”, “restricted”, “notDetermined”, then we will again ask user for authorization for location. Here we requested user to allow our app to access location when app in use only  (requestWhenInUseAuthorization), you can also choose “requestAlwaysAuthorization”, this permission will detect user location always in background mode too . Lastly we set location desired accuracy to best and delegate to self so that we get notified about user location update or failed, then finally we start monitoring for location updates by calling the method “startUpdatingLocation”.
 
                                      Once location is detected, then “didUpdateLocations” delegate of CLLocationManager will be gets called, providing us latitude and longitude. If it failed to get  location then “didFailWithError” method will be invoked. In “didUpdateLocations” method, first we stop the location manger as we need only location for first time. The we create “MKCoordinateRegion” for our mapView, so that we can zoom our map to user current location.
 

Note: In info.plist file, we need to set privacy policy for the location access.

Adding privace key in info.plist file

How to simulate location in xcode simulators

Simulating location in xcode simulators

Where to go from here;

In this tutorial, we learned how to fetch user current location and display it on MKMapView in swift language. You can download the source code from https://goo.gl/ES48XE