Show device location using indoor positioning

View on GitHub

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 are 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-aware 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 and load an IndoorPositioningDefinition (stored with the map), then create an IndoorsLocationDataSource from it. See the details about creating the location data source in the "Additional information" section below.
  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 start() and stop(). Device location will appear on the display as a blue dot and update as the user moves throughout the space.
  6. Use the autoPanMode property to change how the map behaves when location updates are received.

Relevant API

  • ArcGISFeatureTable
  • FeatureTable
  • IndoorPositioningDefinition
  • IndoorsLocationDataSource
  • LocationDisplay
  • LocationDisplay.AutoPanMode
  • Map
  • MapView

About the data

This sample uses an IPS-aware 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.
  • You could initialize the IndoorsLocationDataSource from individual feature tables stored in different data sources, such as map, mobile geodatabase, and feature services. With version 200.5 and higher, you can construct the IndoorsLocationDataSource using a single IndoorPositioningDefinition available in your IPS-aware map. In short…
    • If an IndoorPositioningDefinition is available in your IPS-aware map, use it to construct IndoorsLocationDataSource.
    • If your map does not have an IndoorPositioningDefinition, construct the location data source from individual feature tables as described in Manually create an indoor location data source documentation.
  • 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

ShowDeviceLocationUsingIndoorPositioningView.swiftShowDeviceLocationUsingIndoorPositioningView.swiftShowDeviceLocationUsingIndoorPositioningView.Model.swift
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
// Copyright 2024 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
//
//   https://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.

import ArcGIS
import CoreLocation
import SwiftUI

struct ShowDeviceLocationUsingIndoorPositioningView: View {
    /// The data model for the sample.
    @StateObject private var model = Model()
    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        MapView(map: model.map)
            .locationDisplay(model.locationDisplay)
            .overlay(alignment: .top) {
                HStack(alignment: .center, spacing: 10) {
                    VStack(alignment: .leading) {
                        // The floor number of a location when in a building
                        // where the ground floor is 0. Negative numbers
                        // indicate floors below ground level.
                        if let floor = model.currentFloor {
                            let humanReadableFloor = if floor >= .zero {
                                // Add 1 to above the ground floor levels.
                                floor + 1
                            } else {
                                // Don't change the underground floor levels.
                                floor
                            }
                            Text("Current floor: \(humanReadableFloor)")
                        }
                        if let accuracy = model.horizontalAccuracy {
                            let formatStyle = Measurement<UnitLength>.FormatStyle.measurement(
                                width: .abbreviated,
                                usage: .asProvided,
                                numberFormatStyle: .number.precision(.fractionLength(2))
                            )
                            Text("Accuracy: \(Measurement<UnitLength>(value: accuracy, unit: .meters), format: formatStyle)")
                        }
                    }
                    .opacity(model.currentFloor != nil ? 1 : 0)
                    VStack(alignment: .leading) {
                        let sourceType = model.positionSource == "GNSS" ? "Satellites" : "Transmitters"
                        Text("Data source: \(model.positionSource ?? "None")")
                        Text("Number of \(sourceType): \(model.signalSourceCount ?? 0)")
                    }
                }
                .frame(maxWidth: .infinity)
                .padding(8)
                .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
            }
            .overlay(alignment: .center) {
                if model.isLoading {
                    ProgressView(
                        """
                        Loading
                        indoor data…
                        """
                    )
                    .padding()
                    .background(.ultraThinMaterial)
                    .clipShape(.rect(cornerRadius: 10))
                    .shadow(radius: 50)
                    .multilineTextAlignment(.center)
                }
            }
            .task {
                // Request location permission if it has not yet been determined.
                let locationManager = CLLocationManager()
                if locationManager.authorizationStatus == .notDetermined {
                    locationManager.requestWhenInUseAuthorization()
                }
                do {
                    try await model.loadIndoorsData()
                } catch {
                    self.error = error
                }
                // Start to receive updates from the location data source.
                await model.updateDisplayOnLocationChange()
            }
            .onDisappear {
                Task {
                    await model.locationDisplay.dataSource.stop()
                }
            }
            .errorAlert(presentingError: $error)
    }
}

#Preview {
    ShowDeviceLocationUsingIndoorPositioningView()
}

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