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

Before starting, you should:

  • Sign up for an ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  • Make sure your machine meets the system requirements.
  • Install ArcGIS Runtime for iOS.

Steps

Open a 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.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In Xcode, in the Project Navigator, click AppDelegate.swift.

    3. In the editor, set the apiKey property on the AGSArcGISRuntimeEnvironment with your API key.


      AppDelegate.swift
                                         
      Change line
      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
      // Copyright 2021 Esri
      //
      // Licensed under the Apache License, Version 2.0 (the "License");
      // you may not use this file except in compliance with the License.
      // You may obtain a copy of the License at
      //
      //   https://www.apache.org/licenses/LICENSE-2.0
      //
      // Unless required by applicable law or agreed to in writing, software
      // distributed under the License is distributed on an "AS IS" BASIS,
      // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      // See the License for the specific language governing permissions and
      // limitations under the License.
      
      
      import UIKit
      
      import ArcGIS
      
      
      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
          var window: UIWindow?
      
          func application(_ application: UIApplication,
                           didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
              // Note: it is not best practice to store API keys in source code.
              // The API key is referenced here for the convenience of this tutorial.
      
              AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY"
      
              return true
          }
      
      }
      

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"

    To learn more about requesting user authorization, see the Note in the Location data sources section of the Device location guide topic.

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.

Instances of this class manage the display of device location on a map view: the symbols, animation, auto pan behavior, and so on. Location display is an overlay of the map view, and displays above everything else, including graphics overlays.

The location display does not retrieve location information, that is the job of the associated data source, which provides location updates on a regular basis. In addition to the default system location data source, you can use location providers based on external GPS devices or a simulated location source.

Each map view has its own instance of a location display and instances of location display and location data source are not shared by multiple map views. This allows you to start and stop location display independently on multiple map views without affecting each other.

  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
                                                     
    Remove lineRemove lineRemove lineRemove lineRemove lineRemove lineRemove line
    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
    // Copyright 2020 Esri
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    //   https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    
    
    import UIKit
    
    import ArcGIS
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
    
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
    
            mapView.map = map
    
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.02700,
                    longitude: -118.80500,
                    scale: 72_000
                )
            )
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    
  3. Add code to start AGSLocationDisplay for the map view and assign a AGSLocationDisplayAutoPanMode to center the map at the device location.

    ViewController.swift
                                                  
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    // Copyright 2021 Esri
    //
    // Licensed under the Apache License, Version 2.0 (the "License");
    // you may not use this file except in compliance with the License.
    // You may obtain a copy of the License at
    //
    //   https://www.apache.org/licenses/LICENSE-2.0
    //
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    // See the License for the specific language governing permissions and
    // limitations under the License.
    
    import UIKit
    
    import ArcGIS
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var mapView: AGSMapView!
        private func setupMap() {
    
            let map = AGSMap(
                basemapStyle: .arcGISTopographic
            )
            mapView.map = map
    
            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
            }
    
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
        }
    
    }
    

Run the app

  1. Press <Command+R> to run the project.

    Other ways to run the project in Xcode:

    • In Xcode's app menu, select Product > Run.
    • At the top-left of the Xcode project window, press the Run button.
  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.

Congratulations, you're done!

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

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