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.
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
- Implement the
CustomLocationDataSource.LocationProvider
interface overriding theheadings
andlocations
flows. - Create a
FusedLocationProviderClient
andFusedOrientationProviderClient
to get the device's location and orientation. - Request location and orientation updates from the clients, then emit these values into the
locations
andheadings
flows. Utilize the functioncreateArcGISLocationFromFusedLocation(...)
to convert a fusedLocation
object into anArcGISLocation
object. - Create a
LocationDisplay
withrememberLocationDisplay()
and set it to the composableMapView
. - Set the
LocationDisplay
data source to aCustomLocationDataSource
which implements theLocationProvider
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
/* 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.components
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.location.CustomLocationDataSource
import com.arcgismaps.location.LocationDisplayAutoPanMode
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.view.LocationDisplay
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch
class ShowDeviceLocationUsingFusedLocationDataSourceViewModel(application: Application) :
AndroidViewModel(application) {
// Create an ArcGIS map
val arcGISMap = ArcGISMap(BasemapStyle.ArcGISNavigation)
// Create a message dialog view model for handling error messages
val messageDialogVM = MessageDialogViewModel()
// Create a fused location and fused orientation provider to get location and orientation updates from the fused
// location and fused orientation APIs
private val fusedLocationProvider = FusedLocationAndOrientationProvider(getApplication())
/**
* Pass changes in priority to the fused location provider.
*/
fun onPriorityChanged(priority: Int) {
fusedLocationProvider.onPriorityChanged(priority)
}
/**
* Pass changes in interval to the fused location provider.
*/
fun onIntervalChanged(interval: Long) {
fusedLocationProvider.onIntervalChanged(interval)
}
/**
* Initialize the location display with a custom location data source using the fused location provider.
*/
fun initialize(locationDisplay: LocationDisplay) {
viewModelScope.launch {
arcGISMap.load().onFailure { error ->
messageDialogVM.showMessageDialog(
"Failed to load map", error.message.toString()
)
}
}
// Set the location display to be used by this view model
locationDisplay.apply {
// Set the location display's data source to a Custom Location DataSource which implements a location
// provider interface on the Fused Location API
dataSource = CustomLocationDataSource { fusedLocationProvider }
// Keep track of the job so it can be canceled elsewhere
viewModelScope.launch {
// Start the data source
dataSource.start()
// Start emitting fused locations into the data source
fusedLocationProvider.start()
}
// Set the AutoPan mode to recenter around the location display
setAutoPanMode(LocationDisplayAutoPanMode.CompassNavigation)
}
}
override fun onCleared() {
super.onCleared()
fusedLocationProvider.stop()
}
}