Display Device Location

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

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

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  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, get an access token and set the 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 provides AGSLocationDisplay for showing the current location of the . 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 . The map will zoom to the of the current location, so this code is no longer needed.

    ViewController.swift
    Expand
    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
    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.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close