This post is part of my collection: Swift 2 – For Beginners.
In this example we will see how to get the user’s location and we will update our map with it.
We will use the setMapLocation method that we created in Swift 2 examples – #3 Set the location of a MapView
To get the user’s location we need the CoreLocation.framework, the first thing that we should do is to add it to our project:

Then we import the CoreLocation in our controller and implement CLLocationManagerDelegate
// 2 Import CoreLocation
import CoreLocation
// 3 Implement CLLocationManagerDelegate
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
...
The next step will be to create and run the location manager:
// 4 Create Location manager
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// 5 Configure and start the location manager
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest // GPS
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
It is worth to mention this line locationManager.requestWhenInUseAuthorization().
It will shows a popup asking for permission to access the location, we can show a message within it to explain the user why.

The message that will be shown has to be defined in the Info.plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>This is a message displayed to the user to ask for location permissions.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This is a message displayed to the user to ask for permissions to get the location in background.</string>
If the user allows the location access this method will be called in the delegate when the application detect a location change.
// 6 Called when the phone detect a new location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
MapHelper.setMapLocation(mapView: self.map, location: userLocation.coordinate, zoom: 0.01)
}
The method has as a parameter an array of locations, it contains always at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries.
All the code of this example together:
import UIKit
import MapKit
// 1 Add CoreLocation.framework to the project
// 2 Import CoreLocation
import CoreLocation
// 3 Implement CLLocationManagerDelegate
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
// 4 Create Location manager
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// 5 Configure and start the location manager
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest // GPS
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
// 6 Called when the phone detect a new location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
MapHelper.setMapLocation(mapView: self.map, location: userLocation.coordinate, zoom: 0.01)
}
}
One last thing worth to mention is that the iOS emulator supports different location simulation modes. It helps a lot during development.

Like this:
Like Loading...