Show device location using fused location data source

View on GitHubSample 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

ShowDeviceLocationUsingFusedLocationDataSourceViewModel.ktShowDeviceLocationUsingFusedLocationDataSourceViewModel.ktMainActivity.ktFusedLocationAndOrientationProvider.ktShowDeviceLocationUsingFusedLocationDataSource.kt
Use dark colors for code blocksCopy
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
80
81
82
83
84
85
86
87
88
89
90
91
92
/* 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()
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.