Show device location using indoor positioning

View on GitHubSample viewer app

Show your device's real-time location while inside a building by using signals from indoor positioning beacons.

Show device location using indoor positioning

Use case

An indoor positioning system (IPS) allows you to locate yourself and others inside a building in real time. Similar to GPS, it puts a blue dot on indoor maps and can be used with other location services to help navigate to any point of interest or destination, as well as provide an easy way to identify and collect geospatial information at their location.

How to use the sample

When the device is within range of an IPS beacon, toggle "Show Location" to change the visibility of the location indicator in the map view. The system will ask for permission to use the device's location if the user has not yet used location services in this app. It will then start the location display with auto-pan mode set to navigation.

When there is no IPS beacons nearby, or other errors occur while initializing the indoors location data source, it will seamlessly fall back to the current device location as determined by GPS.

How it works

  1. Load an IPS-enabled map. This can be a web map hosted as a portal item in ArcGIS Online, an Enterprise Portal, or a mobile map package (.mmpk) created with ArcGIS Pro.
  2. Create an IndoorsLocationDataSource with the positioning feature table (stored with the map) and the pathways feature table after both tables are loaded.
  3. Handle location change events to respond to floor changes or read other metadata for locations.
  4. Assign the IndoorsLocationDataSource to the map view's location display.
  5. Enable and disable the map view's location display using LocationDisplay.start() and LocationDisplay.stop(). Device location will appear on the display as a blue dot and update as the user moves throughout the space.
  6. Use the LocationDisplay.AutoPanMode property to change how the map behaves when location updates are received.

Relevant API

  • FeatureTable
  • IndoorsLocationDataSource
  • LocationDataSource
  • LocationDisplay
  • FeatureLayer
  • ArcGISMap
  • MapView

About the data

This sample uses an IPS-enabled web map that displays Building L on the Esri Redlands campus. Please note: you would only be able to use the indoor positioning functionalities when you are inside this building. Swap the web map to test with your own IPS setup.

Additional information

  • Location and Bluetooth permissions are required for this sample.
  • To learn more about IPS, read the Indoor positioning article on ArcGIS Developer website.
  • To learn more about how to deploy the indoor positioning system, read the Deploy ArcGIS IPS article.

Tags

beacon, BLE, blue dot, Bluetooth, building, facility, GPS, indoor, IPS, location, map, mobile, navigation, site, transmitter

Sample Code

MainActivity.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Copyright 2022 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.arcgisruntime.sample.showdevicelocationusingindoorpositioning

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.esri.arcgisruntime.data.FeatureTable
import com.esri.arcgisruntime.data.ServiceFeatureTable
import com.esri.arcgisruntime.data.QueryParameters
import com.esri.arcgisruntime.data.Feature
import com.esri.arcgisruntime.data.Field
import com.esri.arcgisruntime.data.ArcGISFeatureTable
import com.esri.arcgisruntime.data.QueryParameters.OrderBy
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.location.IndoorsLocationDataSource
import com.esri.arcgisruntime.location.LocationDataSource
import com.esri.arcgisruntime.location.LocationDataSource.StatusChangedListener
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.LayerList
import com.esri.arcgisruntime.mapping.view.LocationDisplay
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.portal.Portal
import com.esri.arcgisruntime.portal.PortalItem
import com.esri.arcgisruntime.sample.showdevicelocationusingindoorpositioning.databinding.ActivityMainBinding
import com.esri.arcgisruntime.security.UserCredential
import java.text.DecimalFormat
import java.util.UUID

class MainActivity : AppCompatActivity(), LocationDataSource.LocationChangedListener,
    StatusChangedListener {

    private val TAG = MainActivity::class.java.simpleName

    private var currentFloor: Int? = null

    // Provides an indoor or outdoor position based on device sensor data (radio, GPS, motion sensors).
    private var mIndoorsLocationDataSource: IndoorsLocationDataSource? = null

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val progressBar: ProgressBar by lazy {
        activityMainBinding.progressBar
    }

    private val textView: TextView by lazy {
        activityMainBinding.textView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)
        // check for location permissions
        // if permissions is allowed, the device's current location is shown
        checkPermissions()
    }

    /**
     * Set up the connection between the device and the portal item
     */
    private fun connectToPortal() {
        // load the portal and create a map from the portal item
        val portal = Portal("https://www.arcgis.com/", false)
        val portalItem = PortalItem(portal, "8fa941613b4b4b2b8a34ad4cdc3e4bba")
        setupMap(portalItem)
    }

    /**
     * Set the [mapView] using the [portalItem] then invokes [loadTables]
     */
    private fun setupMap(portalItem: PortalItem) {
        mapView.map = ArcGISMap(portalItem)
        val map = mapView.map
        map.addDoneLoadingListener {
            if (mapView.map.loadStatus == LoadStatus.LOADED) {
                val featureTables = map.tables
                // check if the portalItem contains featureTables
                if (featureTables.isNotEmpty()) {
                    setUpLoadTables(featureTables)
                } else {
                    val message = "Map does not contain feature tables"
                    Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
                    Log.e(TAG, message)
                }
            } else {
                val message = "Error loading map: " + map.loadError.message
                Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
                Log.e(TAG, "Error loading map: " + map.loadError.message)
            }
        }
    }

    /**
     * Set up the ResultsCallback for when all tables are loaded
     */
    private fun setUpLoadTables(featureTables: MutableList<FeatureTable>) {
        // portalItem contains featureTables, so perform load on each featureTable
        loadTables(featureTables, object : ResultsCallback {
            override fun onSuccess() {
                // set up the data source using the feature tables, then start the data source
                setupIndoorsLocationDataSource(featureTables)
            }

            override fun onError(exception: Exception?) {
                val message = "Failed to load feature tables: " + (exception?.message ?: "")
                Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()
                Log.e(TAG, message)
            }
        })
    }

    /**
     * Recursively loads each [featureTables] and calls [resultsCallback] once each table is loaded
     */
    private fun loadTables(
        featureTables: MutableList<FeatureTable>,
        resultsCallback: ResultsCallback
    ) {
        val iterator = featureTables.iterator()
        if (iterator.hasNext()) {
            val table = iterator.next()
            table.addDoneLoadingListener {
                if (table.loadStatus == LoadStatus.LOADED) {
                    // skip the first table from the featureTables list since it is loaded
                    val newFeatureTables = featureTables.subList(1, featureTables.size)
                    // recursively call loadTables() with the updated list of featureTables to load
                    loadTables(newFeatureTables, resultsCallback)
                } else {
                    resultsCallback.onError(table.loadError)
                }
            }
            table.loadAsync()
        } else {
            // once all featureTables is loaded the featureTables list will be empty
            resultsCallback.onSuccess()
        }
    }

    /**
     * Sets up the [mIndoorsLocationDataSource] using the positioningTable
     */
    private fun setupIndoorsLocationDataSource(featureTables: List<FeatureTable>) {
        // positioningTable needs to be present
        val positioningTable = featureTables.firstOrNull { it.tableName.equals("ips_positioning") }

        if (positioningTable != null) {
            val serviceFeatureTable = positioningTable as ServiceFeatureTable
            // when multiple entries are available, IndoorsLocationDataSource constructor function
            // looks up the entry with the most recent date and takes this positioning data
            // set up queryParameters to grab one result.
            val dateCreatedFieldName = getDateCreatedFieldName(serviceFeatureTable.fields)
            if (dateCreatedFieldName == null) {
                val message = "The service table does not contain \"DateCreated\" fields."
                Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()
                Log.e(TAG, message)
                return
            }
            val queryParameters = QueryParameters().apply {
                maxFeatures = 1
                whereClause = "1 = 1"
                // find and sort out the orderByFields by most recent first
                orderByFields.add(
                    OrderBy(
                        dateCreatedFieldName,
                        QueryParameters.SortOrder.DESCENDING
                    )
                )
            }
            // perform search query using the queryParameters
            val resultFuture = serviceFeatureTable.queryFeaturesAsync(queryParameters)
            resultFuture.addDoneListener {
                val featureIterator: Iterator<Feature> = resultFuture.get().iterator()
                // check if serviceFeatureTable contains positioning data
                if (featureIterator.hasNext()) {
                    // The ID that identifies a row in the positioning table.
                    val globalID =
                        featureIterator.next().attributes[serviceFeatureTable.globalIdField].toString()
                    val positioningId = UUID.fromString(globalID)
                    // Setting up IndoorsLocationDataSource with positioning, pathways tables and positioning ID.
                    // positioningTable - the "ips_positioning" feature table from an IPS-enabled map.
                    // pathwaysTable - An ArcGISFeatureTable that contains pathways as per the ArcGIS Indoors Information Model.
                    // Setting this property enables path snapping of locations provided by the IndoorsLocationDataSource.
                    // positioningID - an ID which identifies a specific row in the positioningTable that should be used for setting up IPS.
                    mIndoorsLocationDataSource = IndoorsLocationDataSource(
                        this,
                        serviceFeatureTable,
                        getPathwaysTable(),
                        positioningId
                    )
                    // start the location display (blue dot)
                    startLocationDisplay()
                } else {
                    val message = "The positioning table contain no data."
                    Toast.makeText(this@MainActivity, message, Toast.LENGTH_LONG).show()
                    Log.e(TAG, message)
                }
            }
        } else {
            val message = "Positioning Table not found in FeatureTables"
            Toast.makeText(this, message, Toast.LENGTH_LONG).show()
            Log.e(TAG, message)
        }
    }

    /**
     * Find the exact formatting of the name "DateCreated" in the list of ServiceFeatureTable fields.
     */
    private fun getDateCreatedFieldName(fields: List<Field>): String? {
        val field = fields.find {
            it.name.equals(
                "DateCreated",
                ignoreCase = true
            ) || it.name.equals("Date_Created", ignoreCase = true)
        }
        return field?.name
    }

    /**
     * Retrieves the PathwaysTable
     */
    private fun getPathwaysTable(): ArcGISFeatureTable? {
        return try {
            val pathwaysFeatureLayer =
                mapView.map.operationalLayers.firstOrNull { it.name.equals("Pathways") } as? FeatureLayer
            pathwaysFeatureLayer?.featureTable as? ArcGISFeatureTable
        } catch (e: Exception) {
            // if pathways table not found in map's operationalLayers
            val message = "PathwaysTable not found"
            Toast.makeText(this, message, Toast.LENGTH_LONG).show()
            Log.e(TAG, message)
            null
        }
    }

    /**
     * Invokes when the app is closed or LocationDataSource is stopped.
     */
    private fun stopLocationDisplay() {
        mapView.locationDisplay.stop()
        mapView.locationDisplay.locationDataSource.apply {
            removeStatusChangedListener(this@MainActivity)
            removeLocationChangedListener(this@MainActivity)
        }
    }

    /**
     * Sets up the location listeners, the navigation mode, and display's the devices location as a blue dot
     */
    private fun startLocationDisplay() {
        val locationDisplay: LocationDisplay = mapView.locationDisplay.apply {
            autoPanMode = LocationDisplay.AutoPanMode.NAVIGATION
            locationDataSource = mIndoorsLocationDataSource
        }
        // these listeners will receive location, heading and status updates from the location data source.
        locationDisplay.locationDataSource.apply {
            addStatusChangedListener(this@MainActivity)
            addLocationChangedListener(this@MainActivity)
        }
        // asynchronously start of the location display,
        // which will in-turn start IndoorsLocationDataSource to start receiving IPS updates.
        locationDisplay.startAsync()
    }

    /**
     * Handles the data received on a [locationChangedEvent]
     * like currentFloor, positionSource, transmitterCount, networkCount and horizontalAccuracy
     */
    override fun locationChanged(locationChangedEvent: LocationDataSource.LocationChangedEvent?) {
        // get the location properties of the LocationDataSource
        val locationProperties = locationChangedEvent?.location?.additionalSourceProperties
        if (locationProperties == null) {
            Toast.makeText(
                this,
                "LocationDataSource does not have any property-fields",
                Toast.LENGTH_LONG
            ).show()
            Log.e(TAG, "LocationDataSource does not have any property-fields")
            return
        }
        // retrieve information about the location of the device
        val floor = (locationProperties["floor"] ?: "").toString()
        val positionSource =
            (locationProperties[LocationDataSource.Location.KEY_POSITION_SOURCE] ?: "").toString()
        val transmitterCount = (locationProperties["transmitterCount"] ?: "").toString()
        val networkCount =
            (locationProperties[LocationDataSource.Location.KEY_SATELLITE_COUNT] ?: "").toString()

        // check if current floor hasn't been set or if the floor has changed
        val newFloor = floor.toInt()
        if (currentFloor == null || currentFloor != newFloor) {
            currentFloor = newFloor
            // set up the floor layer with the newFloor
            setupLayers()
        }
        // set up the message with floor properties to be displayed to the textView
        var locationPropertiesMessage =
            "Floor: $floor, Position-source: $positionSource, " +
                "Horizontal-accuracy: " + locationChangedEvent.location.let {
                DecimalFormat(".##").format(
                    it.horizontalAccuracy
                )
            } + "m, "
        if (positionSource == LocationDataSource.Location.POSITION_SOURCE_GNSS) {
            locationPropertiesMessage += "Satellite-count: $networkCount"
        } else if (positionSource == "BLE") {
            locationPropertiesMessage += "Transmitter-count: $transmitterCount"
        }
        textView.text = locationPropertiesMessage
    }

    /**
     * Set up the floor layer when the device location is updated
     */
    private fun setupLayers() {
        // update layer's definition express with the current floor
        val layerList: LayerList = mapView.map.operationalLayers
        layerList.forEach { layer ->
            val name = layer.name
            if (layer is FeatureLayer && (name == "Details" || name == "Units" || name == "Levels")) {
                layer.definitionExpression = "VERTICAL_ORDER = $currentFloor"
            }
        }
    }


    /**
     * Handle status changes of IndoorsLocationDataSource using the [statusChangedEvent]
     */
    override fun statusChanged(statusChangedEvent: LocationDataSource.StatusChangedEvent?) {
        when (statusChangedEvent?.status) {
            LocationDataSource.Status.STARTING -> progressBar.visibility = View.VISIBLE
            LocationDataSource.Status.STARTED -> progressBar.visibility = View.GONE
            LocationDataSource.Status.FAILED_TO_START -> {
                progressBar.visibility = View.GONE
                Log.e(TAG, "Failed to start IndoorsLocationDataSource")
                Toast.makeText(this, "Failed to start IndoorsLocationDataSource", Toast.LENGTH_LONG)
                    .show()
            }
            LocationDataSource.Status.STOPPED -> {
                progressBar.visibility = View.GONE
                stopLocationDisplay()
                Log.e(TAG, "IndoorsLocationDataSource stopped due to an internal error")
                Toast.makeText(
                    this,
                    "IndoorsLocationDataSource stopped due to an internal error",
                    Toast.LENGTH_LONG
                ).show()
            }
        }
    }

    /**
     * Check for location permissions, if not received then request for one
     */
    private fun checkPermissions() {
        val requestCode = 1
        val requestPermissions = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
        if (ContextCompat.checkSelfPermission(
                this,
                requestPermissions[0]
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(this, requestPermissions, requestCode)
        } else {
            // permission already given, so no need to request
            connectToPortal()
        }
    }

    /**
     * Result of the user from location permissions request
     */
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // if location permissions accepted, start setting up IndoorsLocationDataSource
            connectToPortal()
        } else {
            val message = "Location permission is not granted"
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
            Log.e(TAG, message)
            progressBar.visibility = View.GONE
        }
    }

    internal interface ResultsCallback {
        fun onSuccess()
        fun onError(exception: Exception?)
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onDestroy() {
        stopLocationDisplay()
        mapView.dispose()
        super.onDestroy()
    }
}

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