Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to find and track your device location on a map.

Using the ArcGIS API for iOS, it's easy to create an app that displays and tracks a device location on any style map. This can be very useful when building applications that allow you to find directions or places around you. This tutorial uses the AGSCLLocationDataSource class provided for this purpose. This class uses Apple's Core Location services to find and update the device's location. To show your location on the map, you can use a default symbol or you can choose your own. Once the location is found, the view animates to the location and can (optionally) rotate according to the direction where the tracked device is heading.

In this tutorial, you will use the ArcGIS API for iOS to build a simple mapping app to track your device location on a map.

Before you begin

Make sure you have installed the latest version of Xcode.

Reuse the starter project

If you have completed the Create a starter app tutorial, then copy the project into a new empty folder. Otherwise, download and unzip the project solution. Open the .xcodeproj file in Xcode. Run and verify the map displays in the device simulator.

Steps

  1. Update info.plist to allow GPS location access. iOS will ask your user if it is OK to grant location access to your app. Right-click on info.plist in the Project Navigator and Open As > / Source Code. Add a new entry just after the opening top-level <dict> tag with the following XML:

     <!-- ADD -->
     <key>NSLocationWhenInUseUsageDescription</key>
     <string>Show your location on a map</string>
    
  2. In ViewController.swift file, create a new method named setupLocationDisplay. This method handles everything your app requires to configure and start the location services. You will add more code to this method in subsequent steps.

    func setupLocationDisplay() {
        mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanMode.compassNavigation
    }
    
  3. Start the location display. Location display is not on by default, you are required to start it and stop it when you no longer require location services.

    func setupLocationDisplay() {
        mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanMode.compassNavigation
        /* ** ADD ** */
        mapView.locationDisplay.start { [weak self] (error:Error?) -> Void in
            if let error = error {
                self?.showAlert(withStatus: error.localizedDescription)
            }
        }
    
  4. Add a new method to show the user an alert message when location access is not granted.

     private func showAlert(withStatus: String) {
         let alertController = UIAlertController(title: "Alert", message:
             withStatus, preferredStyle: UIAlertControllerStyle.alert)
         alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
         present(alertController, animated: true, completion: nil)
     }
    
  5. Call the setupLocationDisplay method from the viewDidLoad method, but after the map is set up.

        setupMap()
        /* ** ADD ** */
        setupLocationDisplay()
    

Congratulations, you're done!

Your map should now display the location of your device on the basemap. It is best to test this on a real device so that the GPS is used. Otherwise the simulator will simulate a device location. Compare your solution with our completed solution project.

Challenge

Use a different basemap

Instead of the basemap selected here, use one of the other basemaps or use the web map you created in the Display a web map tutorial.

Experiment with pan mode

Review the options for AGSLocationDisplay.autoPanMode and experiment with the different settings. Review and experiment with other available properties such as initial zoom scale, wander extent factor, and show accuracy.

Improve the UI

Implement a UI to give the user the ability to stop and start GPS tracking.