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 API keys. If you don't have an account, sign up for free.
A development and deployment environment that meets the system requirements.
An IDE for Android development in Kotlin.
Steps
Open an Android Studio project
To start this tutorial, complete the Display a map tutorial. Or download and unzip the Display a map solution in a new folder.
Modify the old project for use in this new tutorial. Expand More info for instructions.
On your file system, delete the .idea folder, if present, at the top level of your project.
In the Android tool window, open app > res > values > strings.xml.
In the <string name="app_name"> element, change the content to Display device location.
strings.xml
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
<resources><stringname="app_name">Display device location</string><stringname="location_permissions_denied">LocationDisplay cannot run because location permissions were denied.</string></resources>
Create a new element: <string name ="location_permissions_denied">. As its content, add an error message saying that location permissions were denied.
strings.xml
Use dark colors for code blocks
1
2
3
4
5
6
7
<resources><stringname="app_name">Display device location</string><stringname="location_permissions_denied">LocationDisplay cannot run because location permissions were denied.</string></resources>
In the Android tool window, open Gradle Scripts > settings.gradle.
Change the value of rootProject.name to "Display device location".
Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.
If you downloaded the Display a map solution project, get your API key and set it in your app.
An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.
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.
In Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.
In the setApiKey() function, find the ApiKey.create() call and paste your API key inside the quotes, replacing YOUR_API_KEY.
MainActivity.kt
Use dark colors for code blocks
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
privatefunsetApiKey() {
// It is not best practice to store API keys in source code. We have you insert one here// to streamline this tutorial. ArcGISEnvironment.apiKey = ApiKey.create("YOUR_API_KEY")
}
Add import statements
Replace app-specific import statements with the imports (highlighted in yellow) needed for this tutorial.
In MainActivity.kt, create the requestPermissions() method.
Check whether your app already has permissions for accessing fine location and coarse location by calling checkSelfPermission(). If one or both permissions have not been granted, then request permission from the user by calling ActivityCompat.requestPermissions().
Override the onRequestPermissionsResult() callback inherited from the AppCompatActivity class.
If the grant results array indicates that location permissions were granted, then start the location display.
If the locationDisplay.dataSource.start() call in setupMap() fails to start the location data source, the onFailure's lambda gets executed. (You will edit setupMap() and add that lambda in the next section of this tutorial: Show the current location.)
The lambda calls this app's requestPermissions() method, which ultimately results in onRequestPermissionsResult() being called. At this point, you verified location permissions, so it's time for another attempt to start location display.
Each map view has its own instance of a LocationDisplay for showing the current location (point) of the device. The location is displayed as an overlay 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.
In setupMap, delete the code that sets the map's initial viewpoint.
In setupMap(), set the ArcGISEnvironment.applicationContext property and set a LocationDisplayAutoPanMode that centers the map at the device location. Then try to start displaying the location as follows: call the data source's start() method inside the lifecycleScope.launch coroutine builder and listen for success or failure of the start attempt. In the onFailure lambda, which will be invoked when the location data source fails to start, call the request permissions method so the user can approve location permissions.
When you build and run an app in Android Studio, you must first select a device. From the Android Studio toolbar, you can access the drop-down list of your currently available devices, both virtual and physical.
.
If you cannot access the list on the toolbar, click Tools > Device Manager.
You should see your current location displayed on the map. Different location symbols are used depending on the auto pan mode and whether a location is acquired. See LocationDisplayAutoPanMode for details.
By default, a round blue symbol is used to display the device's location. The location data source tries to get the most accurate location available but depending upon signal strength, satellite positions, and other factors, the location reported could be an approximation. A semi-transparent circle around the location symbol indicates the range of accuracy. As the device moves and location updates are received, the location symbol will be repositioned on the map.