Esri ArcGIS SDK v100.x.x Android simple code snippet to enable the current location with live navigation (using Java/Kotlin).
This is successfully working on Android Studio 3.6.1 using Build-Tool 29.0.2, kotlin_version 1.3.70, Java 8 and Gradle 3.6.1.
Kotlin Code to enable ArcGIS MapView to automatically show live current location:
mapView.locationDisplay.startAsync()
mapView.locationDisplay.autoPanMode = LocationDisplay.AutoPanMode.NAVIGATION
It's equivalent Java Code:
mapView.getLocationDisplay().startAsync();
mapView.getLocationDisplay().setAutoPanMode(LocationDisplay.AutoPanMode.NAVIGATION);
Add Manifest Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Get Current Location Coordinates (Lat-Lng) and zoom on it:
// Check if location provider enabled.
val locationServiceManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isEnabled = locationServiceManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
if (!isEnabled) {
// Location provide not enabled.
Toast.makeText(this, "Enable Location Setting", Toast.LENGTH_SHORT).show()
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
} else {
// Fetch valid current location provided from provider.
if (mapView.locationDisplay != null)
if (mapView.locationDisplay.location != null)
if (mapView.locationDisplay.location.position != null) {
val point = mapView.locationDisplay.location.position
// Zoom to current location with magnification 1000.
mapView.setViewpointCenterAsync(point, 1000.0)
Log.d("Latitude", "${point.x}")
Log.d("Longitude", "${point.y}")
}
}
Important Notes:
Here we are using Android Esri ArcGIS v100.7.x
using Java/Kotlin on Android Studio.
The app should have granted for the above two mentioned permission.
The device should have GPS_PROVIDER
enabled. You can also use NETWORK_PROVIDER
in addition to GPS_PROVIDER
.
Instead of LocationDisplay.AutoPanMode.NAVIGATION
try using other options to best meet your use case.
Hope this helped.