Display Device Location
Learn how to display the current device location on a map or scene.
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:
- An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
Steps
Open the Xcode project
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the
.xcodeproj
file in Xcode.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.
In Xcode, in the Project Navigator, click Info.plist.
Add key
NSLocation
of typeWhen In U s e Usage Description 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.
In Xcode, in the Project Navigator, click ViewController.swift.
In the editor, remove code from
setup
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.Map() ViewController.swiftUse dark colors for code blocks Remove line Remove line Remove line Remove line Remove line Remove line Remove line Add code to start
AGSLocationDisplay
for the map view and assign aAGSLocationDisplayAutoPanMode
to center the map at the device location.ViewController.swiftUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.
Run the app
Press <Command+R> to run the app.
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.
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: