Getting the user’s location — iOS #15

Andy O'Sullivan
4 min readMay 12, 2017
icons from the awesome icons8.com

In the previous tutorial Basic Maps — iOS #8, we learned how to put a map into an app and how to add annotations (pins), along with how to move around the map programmatically.

Today, we’ll learn how to get the user’s location and zoom the map to that location.

The stages:

  • Add a Map Kit View object to a View Controller
  • Add the MapKit framework to your ViewController.swift file
  • Use the CLLocationManagerDelegate delegate functions to get the user’s location
  • Edit the Info.plist to ask the user’s permission to use their location

Let’s go!

Open the Main.storyboard and add a Map Kit View to the View Controller:

Resize and position as needed and also add a Button:

Open the Assistant Editor to view the ViewController.swift file beside the Storyboard and add an Outlet for the Map Kit View, I call mine “mapView”:

which results in:

Also add an Action Outlet for the Button, I call it “onZoomToUserButton” — because that’s what will happen later when the user hits the button; the map will zoom to the user’s location:

Now, add this import to the start of the Class:

import MapKit

here:

import UIKit
import MapKit
class ViewController: UIViewController {

This is mandatory — or the Class won’t recognise the Map Kit View.

Now, add a new delegate — UILocationManagerDelegate like so:

import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView!

We use a CLLocationManager to get the user’s location — and we use the CLLocationManagerDelegate to help us.

We need an instance of LocationManager so add one:

class ViewController: UIViewController, CLLocationManagerDelegate  {    @IBOutlet weak var mapView: MKMapView!    var locationManager = CLLocationManager()

The CLLocationManager works as such:

  • You “start” it
  • It automatically calls a function didUpdateLocations with the user location
  • It keeps calling this function until you “stop” the CLLocationManager.

So, let’s “start” it in the onZoomToUserButton function:

@IBAction func onZoomToUserButton(_ sender: Any) {if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
} else {
locationManager.requestWhenInUseAuthorization()
}
}

This checks if the user has given permission to user their location; if so it runs locationManager.startUpdatingLocation() to start the LocationManager, otherwise it asks the user for permission.

Now add the delegate function didUpdateLocations:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.first!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500) mapView.setRegion(coordinateRegion, animated: true) locationManager.stopUpdatingLocation()
}

This creates a map region from the user’s location and zooms the map to the point. The “500,500” bit is the scale — change these values to make the map appear more zoomed in / out as required.

Almost done — first add this line to viewDidLoad:

override func viewDidLoad() {   super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self}

and finally, we need to add a row to the Info.plist to ask the user’s permission. This is located near the bottom of the list of files in the Project Explorer — click on it to open:

Click on the + sign on the last row and you’ll see a new row appear with a dropdown:

Scroll down and pick Privacy — Location When In Use Usage Description and then over on the right, type in a reason that the user will see on screen, like “So we can track you!” or whatever you want!

Run it! You should see this:

Hit that button! You should see this:

Hit Allow and it should zoom to your location:

Boom! Move the map around — then hit the button again and you’ll be zoomed back to your location.

Any issues let me know!

If you found useful, please hit the little recommend button! Andy

--

--