View on GitHub Sample viewer app

This sample demonstrates how to use the Fused Location Provider and Fused Orientation Provider to implement an ArcGIS Maps SDK Custom Location Data Source Location Provider.

Image of show device location using fused location data source

Use case

The Fused Location Provider can provide more accurate location information than a single location provider. It uses GPS, Wi-Fi, and cell network data to determine the device’s location. In urban areas, it can also use 3D building data in urban areas to improve GPS accuracy. Similarly, the Fused Orientation Provider uses a fusion of magnetometer, accelerometer, and gyroscope data to provide a more accurate device orientation.

How to use the sample

Start the sample and allow the app to access your device’s location. The sample will display your location on the map. Use the priority and interval settings to change the location provider’s behavior. Note the change in the location display when changing these settings—namely the change in the rate at which the expanding blue ring animation triggers (which signifies an updated location).

How it works

  1. Implement the CustomLocationDataSource.LocationProvider interface overriding the headings and locations flows.
  2. Create a FusedLocationProviderClient and FusedOrientationProviderClient to get the device’s location and orientation.
  3. Request location and orientation updates from the clients, then emit these values into the locations and headings flows. Utilize the function createArcGISLocationFromFusedLocation(...) to convert a fused Location object into an ArcGISLocation object.
  4. Create a LocationDisplay with rememberLocationDisplay() and set it to the composable MapView.
  5. Set the LocationDisplay data source to a CustomLocationDataSource which implements the LocationProvider interface.

Relevant API

  • CustomLocationDataSource
  • Location
  • LocationDataSource
  • LocationDisplay
  • LocationProvider

Additional information

The fused location and orientation APIs are part of Google Play Services. The fused location provider intelligently combines different signals, such as GPS and Wi-Fi, to provide location information. The fused orientation provider is a new API that allows users to access orientation information on Android devices.

Tags

cell, fused, GPS, headings, locations, orientation, Wifi

Sample Code

MainActivity.kt MainActivity.kt FusedLocationAndOrientationProvider.kt ShowDeviceLocationUsingFusedLocationDataSourceViewModel.kt ShowDeviceLocationUsingFusedLocationDataSource.kt
/* Copyright 2025 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
*
* http://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.
*
*/
package com.esri.arcgismaps.sample.showdevicelocationusingfusedlocationdatasource
import android.Manifest
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.showdevicelocationusingfusedlocationdatasource.screens.ShowDeviceLocationUsingFusedLocationDataSource
class MainActivity : ComponentActivity() {
private var isLocationPermissionGranted = false
private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
isLocationPermissionGranted = true
} else {
Toast.makeText(this, "Location permission is required to run this sample!", Toast.LENGTH_SHORT).show()
}
enableEdgeToEdge()
setContent {
SampleAppTheme {
ShowDeviceLocationUsingFusedLocationDataSource()
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
ArcGISEnvironment.applicationContext = applicationContext
requestLocationPermission()
}
private fun requestLocationPermission() {
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
@Composable
private fun ShowDeviceLocationUsingFusedLocationDataSource() {
Surface(color = MaterialTheme.colorScheme.background) {
ShowDeviceLocationUsingFusedLocationDataSource(
sampleName = getString(R.string.show_device_location_using_fused_location_data_source),
locationPermissionGranted = isLocationPermissionGranted
)
}
}
}