Display Device Location

Learn how to display the current device location on a map or scene.

display device location

You can display the device location on a map or scene. This is important for workflows that require the user's current location, such as finding nearby businesses, navigating from the current location, or identifying and collecting geospatial information.

By default, location display uses the device's location provider. Your app can also process input from other location providers, such as an external GPS receiver or a provider that returns a simulated location. For more information, see the Show device location topic.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.

Steps

Open the Xcode project

  1. To start the tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the .xcodeproj file in Xcode.

  3. If you downloaded the solution project, set your API key.

Request user authorization to use location

The app must request authorization from the user to access the device location. The user can grant or deny the request.

  1. In Xcode, in the Project Navigator, click Info.plist.

  2. Add key NSLocationWhenInUseUsageDescription of type String. Add a string value describing how the app would use the device location. For example, "To show your location on map"

Show the current location

A map view provides AGSLocationDisplay for showing the current location of the device. The location symbol is displayed on top of all content in the map view.

  1. In Xcode, in the Project Navigator, click ViewController.swift.

  2. In the editor, remove code from setupMap() method that sets the map's initial viewpoint. The map will zoom to the extent of the current location, so this code is no longer needed.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
    Expand
  3. Add code to start AGSLocationDisplay for the map view and assign a AGSLocationDisplayAutoPanMode to center the map at the device location.

    ViewController.swift
    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
            self.mapView.locationDisplay.start {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                self.mapView.locationDisplay.autoPanMode = .recenter
            }
    
    Expand

Run the app

  1. Press Command + R to run the app.

  2. When the app runs, you'll see the system prompt requesting to use the device location. Tap to either Allow once or While using the app.

  3. If the app is running on a simulator, you'll have to provide a simulated location. Go to the Features menu of the Simulator. Under Location, choose a predefined option or enter a custom Location.

Your map should now show the device's location, either simulated or actual.

What's next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.