Get User's Current Location / Coordinates Get User's Current Location / Coordinates ios ios

Get User's Current Location / Coordinates


To get a user's current location you need to declare:

let locationManager = CLLocationManager()

In viewDidLoad() you have to instantiate the CLLocationManager class, like so:

// Ask for Authorisation from the User.self.locationManager.requestAlwaysAuthorization() // For use in foregroundself.locationManager.requestWhenInUseAuthorization()if CLLocationManager.locationServicesEnabled() {    locationManager.delegate = self    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters    locationManager.startUpdatingLocation()}

Then in CLLocationManagerDelegate method you can get user's current location coordinates:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }    print("locations = \(locValue.latitude) \(locValue.longitude)")}

In the info.plist you will have to add NSLocationAlwaysUsageDescriptionand your custom alert message like; AppName(Demo App) would like to use your current location.


you should do those steps:

  1. add CoreLocation.framework to BuildPhases -> Link Binary With Libraries (no longer necessary as of XCode 7.2.1)
  2. import CoreLocation to your class - most likely ViewController.swift
  3. add CLLocationManagerDelegate to your class declaration
  4. add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to plist
  5. init location manager:

    locationManager = CLLocationManager()locationManager.delegate = self;locationManager.desiredAccuracy = kCLLocationAccuracyBestlocationManager.requestAlwaysAuthorization()locationManager.startUpdatingLocation()
  6. get User Location By:

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {    let locValue:CLLocationCoordinate2D = manager.location!.coordinate    print("locations = \(locValue.latitude) \(locValue.longitude)")}


Update for iOS 12.2 with Swift 5

you must add following privacy permissions in plist file

<key>NSLocationWhenInUseUsageDescription</key><string>Description</string><key>NSLocationAlwaysAndWhenInUseUsageDescription</key><string>Description</string><key>NSLocationAlwaysUsageDescription</key><string>Description</string>

Here is how I am

getting current location and showing on Map in Swift 2.0

Make sure you have added CoreLocation and MapKit framework to your project (This doesn't required with XCode 7.2.1)

import Foundationimport CoreLocationimport MapKitclass DiscoverViewController : UIViewController, CLLocationManagerDelegate {    @IBOutlet weak var map: MKMapView!    var locationManager: CLLocationManager!    override func viewDidLoad()    {        super.viewDidLoad()        if (CLLocationManager.locationServicesEnabled())        {            locationManager = CLLocationManager()            locationManager.delegate = self            locationManager.desiredAccuracy = kCLLocationAccuracyBest            locationManager.requestAlwaysAuthorization()            locationManager.startUpdatingLocation()        }    }    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])    {        let location = locations.last! as CLLocation        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))        self.map.setRegion(region, animated: true)    }}

Here is the result screen

a screenshot of the map, centered in Mumbai