Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

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

Using the ArcGIS Runtime SDK for iOS, it's easy to create an app that displays and tracks the location of a device 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 map view's AGSLocationDisplay class provided for this purpose. Under the hood, the location display's data source 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 Runtime SDK 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:

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

    /** ADD **/
    func setupLocationDisplay() {
        mapView.locationDisplay.autoPanMode = .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 = .compassNavigation
        /** ADD **/
        mapView.locationDisplay.start { [weak self] (error) in
            if let error = error {
                self?.showError(error)
            }
        }
    }
    
  4. Add a new method to show the user an alert message when location access is not granted.

    /** ADD **/
    private func showError(_ error: Error) {
        let alert = UIAlertController(
            title: nil,
            message: error.localizedDescription,
            preferredStyle: .alert
        )
        let dismiss = UIAlertAction(
            title: "Dismiss",
            style: .default,
            handler: nil
        )
        alert.addAction(dismiss)
        present(alert, animated: true, completion: nil)
    }
    
  5. Call the setupLocationDisplay method from the viewDidLoad method, after setupMap.

    override func viewDidLoad() {
        super.viewDidLoad()
        setupMap()
        /** ADD **/
        setupLocationDisplay()
    }
    
  6. Press Command-R to run the app in the iOS Simulator.

    (Note, as of 100.8 Runtime supports Metal. In order to run your app in a simulator you must meet some minimum requirements. You must be developing on macOS Catalina, using Xcode 11, and simulating iOS 13.)

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.