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

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
- 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.
- Create an
IndoorsLocationDataSourcewith the positioning feature table (stored with the map) and the pathways feature table after both tables are loaded. - Handle location change events to respond to floor changes or read other metadata for locations.
- Set the
IndoorsLocationDataSourceto the map view’s location display. - Set the auto pan mode to
Navigationto zoom to and follow the user’s location. - Enable the map view’s location display using
LocationDisplay::start(). Device location will appear on the display as a blue dot and update as the user moves throughout the space.
Relevant API
- ArcGISFeatureTable
- FeatureTable
- IndoorsLocationDataSource
- LocationDisplay
- LocationDisplayAutoPanMode
- Map
- 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
// [WriteFile Name=ShowDeviceLocationUsingIndoorPositioning, Category=Maps]// [Legal]// 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.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "ShowDeviceLocationUsingIndoorPositioning.h"
// ArcGIS Maps SDK headers#include "Error.h"#include "FeatureTable.h"#include "FeatureTableListModel.h"#include "FloorLevel.h"#include "FloorManager.h"#include "IndoorPositioningDefinition.h"#include "IndoorsLocationDataSource.h"#include "LocationDisplay.h"#include "LocationSourcePropertiesKeys.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"#include "PortalItem.h"
// Qt headers#include <QMetaObject>#include <QPermissions>
// Platform specific headers#ifdef Q_OS_ANDROID#include "ArcGISRuntimeEnvironment.h"
#include <QCoreApplication>#include <QJniObject>#endif
using namespace Esri::ArcGISRuntime;
namespace{ const QString itemId = "8fa941613b4b4b2b8a34ad4cdc3e4bba";} // namespace
ShowDeviceLocationUsingIndoorPositioning::ShowDeviceLocationUsingIndoorPositioning(QObject* parent /* = nullptr */) : QObject(parent){#ifdef Q_OS_ANDROID ArcGISRuntimeEnvironment::setAndroidApplicationContext(QJniObject{QNativeInterface::QAndroidApplication::context()});#endif
m_map = new Map(new PortalItem(itemId, this), this); connect(m_map, &Map::doneLoading, this, [this](const Error& loadError) { if (!loadError.isEmpty()) { failSetup(loadError.message()); return; }
trySetupIndoorsLocationDataSource(); });}
ShowDeviceLocationUsingIndoorPositioning::~ShowDeviceLocationUsingIndoorPositioning() = default;
void ShowDeviceLocationUsingIndoorPositioning::stopLocationDisplay(){ if (m_mapView) { m_mapView->locationDisplay()->stop(); }}
void ShowDeviceLocationUsingIndoorPositioning::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<ShowDeviceLocationUsingIndoorPositioning>("Esri.Samples", 1, 0, "ShowDeviceLocationUsingIndoorPositioningSample");}
MapQuickView* ShowDeviceLocationUsingIndoorPositioning::mapView() const{ return m_mapView;}
bool ShowDeviceLocationUsingIndoorPositioning::isLoading() const{ return m_isLoading;}
// Set the view (created in QML)void ShowDeviceLocationUsingIndoorPositioning::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) { return; }
m_mapView = mapView; m_mapView->setMap(m_map);
// workaround for https://bugreports.qt.io/browse/QTBUG-134211 QMetaObject::invokeMethod(this, [this]() { requestBluetoothThenLocationPermissions(); }, Qt::QueuedConnection);
emit mapViewChanged();}
void ShowDeviceLocationUsingIndoorPositioning::requestBluetoothThenLocationPermissions(){ qApp->requestPermission(QBluetoothPermission{}, [this](const QPermission& permission) { Q_UNUSED(permission); requestLocationPermissionThenSetupILDS(); });}
void ShowDeviceLocationUsingIndoorPositioning::requestLocationPermissionThenSetupILDS(){ QLocationPermission locationPermission{}; locationPermission.setAccuracy(QLocationPermission::Accuracy::Precise); locationPermission.setAvailability(QLocationPermission::Availability::WhenInUse); qApp->requestPermission(locationPermission, [this](const QPermission& permission) { Q_UNUSED(permission); validatePermissions(); trySetupIndoorsLocationDataSource(); });}
void ShowDeviceLocationUsingIndoorPositioning::validatePermissions(){ bool bluetoothPermissionGranted = false; bool permissionsDenied = false; if (const Qt::PermissionStatus status = qApp->checkPermission(QBluetoothPermission{}); status == Qt::PermissionStatus::Denied) { permissionsDenied = true; emit bluetoothPermissionDenied(); } else if (status == Qt::PermissionStatus::Granted) { bluetoothPermissionGranted = true; }
bool locationPermissionGranted = false; QLocationPermission locationPermission{}; locationPermission.setAccuracy(QLocationPermission::Accuracy::Precise); locationPermission.setAvailability(QLocationPermission::Availability::WhenInUse); if (const Qt::PermissionStatus status = qApp->checkPermission(locationPermission); status == Qt::PermissionStatus::Denied) { permissionsDenied = true; emit locationPermissionDenied(); } else if (status == Qt::PermissionStatus::Granted) { locationPermissionGranted = true; }
if (bluetoothPermissionGranted && locationPermissionGranted) { m_allPermissionsGranted = true; } else if (permissionsDenied) { setLoading(false); }}
void ShowDeviceLocationUsingIndoorPositioning::setLoading(bool isLoading){ if (m_isLoading == isLoading) { return; }
m_isLoading = isLoading; emit isLoadingChanged();}
void ShowDeviceLocationUsingIndoorPositioning::trySetupIndoorsLocationDataSource(){ if (!m_mapView || m_setupFailed || m_isSettingUpIndoors || m_indoorsLocationDataSource) { return; }
// we must have both the map loaded and all permissions granted if (m_map->loadStatus() != LoadStatus::Loaded || !m_allPermissionsGranted) { return; }
IndoorPositioningDefinition* indoorPositioningDef = m_map->indoorPositioningDefinition(); if (!indoorPositioningDef) { failSetup(tr("Cannot initialize indoors location data source.")); return; }
m_isSettingUpIndoors = true; setLoading(true); m_pendingPrerequisiteLoads = 0;
for (FeatureTable* table : *m_map->tables()) { if (!table) { continue; }
if (table->loadStatus() == LoadStatus::Loaded) { continue; }
if (table->loadStatus() == LoadStatus::FailedToLoad) { failSetup(table->loadError().message()); return; }
++m_pendingPrerequisiteLoads; connect(table, &FeatureTable::doneLoading, this, &ShowDeviceLocationUsingIndoorPositioning::handlePrerequisiteDoneLoading); if (table->loadStatus() == LoadStatus::NotLoaded) { table->load(); } }
if (FloorManager* floorManager = m_map->floorManager()) { if (floorManager->loadStatus() == LoadStatus::FailedToLoad) { failSetup(floorManager->loadError().message()); return; }
if (floorManager->loadStatus() != LoadStatus::Loaded) { ++m_pendingPrerequisiteLoads; connect(floorManager, &FloorManager::doneLoading, this, &ShowDeviceLocationUsingIndoorPositioning::handlePrerequisiteDoneLoading); if (floorManager->loadStatus() == LoadStatus::NotLoaded) { floorManager->load(); } } }
if (indoorPositioningDef->loadStatus() == LoadStatus::FailedToLoad) { failSetup(indoorPositioningDef->loadError().message()); return; }
if (indoorPositioningDef->loadStatus() != LoadStatus::Loaded) { ++m_pendingPrerequisiteLoads; connect(indoorPositioningDef, &IndoorPositioningDefinition::doneLoading, this, &ShowDeviceLocationUsingIndoorPositioning::handlePrerequisiteDoneLoading); if (indoorPositioningDef->loadStatus() == LoadStatus::NotLoaded) { indoorPositioningDef->load(); } }
if (m_pendingPrerequisiteLoads == 0) { completeIndoorsLocationSetup(); }}
void ShowDeviceLocationUsingIndoorPositioning::handlePrerequisiteDoneLoading(const Error& loadError){ if (m_setupFailed || !m_isSettingUpIndoors) { return; }
if (!loadError.isEmpty()) { failSetup(loadError.message()); return; }
if (m_pendingPrerequisiteLoads > 0) { --m_pendingPrerequisiteLoads; }
if (m_pendingPrerequisiteLoads == 0) { completeIndoorsLocationSetup(); }}
void ShowDeviceLocationUsingIndoorPositioning::completeIndoorsLocationSetup(){ if (m_setupFailed || m_indoorsLocationDataSource || !m_mapView) { return; }
IndoorPositioningDefinition* indoorPositioningDef = m_map->indoorPositioningDefinition(); if (!indoorPositioningDef || indoorPositioningDef->loadStatus() != LoadStatus::Loaded) { failSetup(tr("Cannot initialize indoors location data source.")); return; }
showGroundFloor(); m_hasReceivedFirstLocation = false;
m_indoorsLocationDataSource = new IndoorsLocationDataSource(indoorPositioningDef, this); LocationDisplay* locationDisplay = m_mapView->locationDisplay();
connect(locationDisplay, &LocationDisplay::locationChanged, this, &ShowDeviceLocationUsingIndoorPositioning::locationChangedHandler);
locationDisplay->setDataSource(m_indoorsLocationDataSource); locationDisplay->setAutoPanMode(LocationDisplayAutoPanMode::CompassNavigation); locationDisplay->start(); m_isSettingUpIndoors = false;}
void ShowDeviceLocationUsingIndoorPositioning::showGroundFloor(){ FloorManager* floorManager = m_map->floorManager(); if (!floorManager || floorManager->loadStatus() != LoadStatus::Loaded) { return; }
for (FloorLevel* level : floorManager->levels()) { if (level) { level->setVisible(level->verticalOrder() == 0); } }}
void ShowDeviceLocationUsingIndoorPositioning::failSetup(const QString& errorMessage){ if (m_setupFailed) { return; }
m_setupFailed = true; m_isSettingUpIndoors = false; m_pendingPrerequisiteLoads = 0; setLoading(false); emit errorOccurred(errorMessage.isEmpty() ? tr("Cannot initialize indoors location data source.") : errorMessage);}
QVariantMap ShowDeviceLocationUsingIndoorPositioning::locationProperties() const{ return m_locationProperties;}
// Change currently displayed location information and change floor display if necessaryvoid ShowDeviceLocationUsingIndoorPositioning::locationChangedHandler(const Location& loc){ m_locationProperties = loc.additionalSourceProperties(); m_locationProperties["horizontalAccuracy"] = QVariant::fromValue(loc.horizontalAccuracy());
if (!m_hasReceivedFirstLocation) { m_hasReceivedFirstLocation = true; setLoading(false); }
const QVariant floor = m_locationProperties.value(LocationSourcePropertiesKeys::floor()); const QString floorLevelId = m_locationProperties.value(LocationSourcePropertiesKeys::floorLevelId()).toString(); if (floor.isValid() && !floorLevelId.isEmpty()) { changeFloorDisplay(floorLevelId); }
emit locationPropertiesChanged();}
void ShowDeviceLocationUsingIndoorPositioning::changeFloorDisplay(const QString& floorLevelId){ FloorManager* floorManager = m_map->floorManager(); if (!floorManager || floorManager->loadStatus() != LoadStatus::Loaded) { return; }
for (FloorLevel* level : floorManager->levels()) { if (level) { level->setVisible(level->levelId() == floorLevelId); } }}// [WriteFile Name=ShowDeviceLocationUsingIndoorPositioning, Category=Maps]// [Legal]// 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.// [Legal]
#ifndef SHOWDEVICELOCATIONUSINGINDOORPOSITIONING_H#define SHOWDEVICELOCATIONUSINGINDOORPOSITIONING_H
// ArcGIS Maps SDK headers#include "Location.h"
// Qt headers#include <QMap>#include <QObject>
namespace Esri::ArcGISRuntime{ class Error; class Map; class MapQuickView; class IndoorsLocationDataSource;} // namespace Esri::ArcGISRuntime
Q_MOC_INCLUDE("MapQuickView.h")
class ShowDeviceLocationUsingIndoorPositioning : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(QVariantMap locationProperties READ locationProperties NOTIFY locationPropertiesChanged) Q_PROPERTY(bool isLoading READ isLoading NOTIFY isLoadingChanged)
public: explicit ShowDeviceLocationUsingIndoorPositioning(QObject* parent = nullptr); ~ShowDeviceLocationUsingIndoorPositioning();
static void init();
Q_INVOKABLE void stopLocationDisplay();
signals: void mapViewChanged(); void locationPropertiesChanged(); void isLoadingChanged(); void locationPermissionDenied(); void bluetoothPermissionDenied(); void errorOccurred(const QString& errorMessage);
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; bool isLoading() const;
void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); void trySetupIndoorsLocationDataSource(); void locationChangedHandler(const Esri::ArcGISRuntime::Location& loc); void changeFloorDisplay(const QString& floorLevelId); void requestLocationPermissionThenSetupILDS(); void requestBluetoothThenLocationPermissions(); void validatePermissions(); void setLoading(bool isLoading); void handlePrerequisiteDoneLoading(const Esri::ArcGISRuntime::Error& loadError); void completeIndoorsLocationSetup(); void showGroundFloor(); void failSetup(const QString& errorMessage); QVariantMap locationProperties() const;
bool m_allPermissionsGranted = false; bool m_hasReceivedFirstLocation = false; bool m_isLoading = true; bool m_isSettingUpIndoors = false; bool m_setupFailed = false; int m_pendingPrerequisiteLoads = 0; Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::IndoorsLocationDataSource* m_indoorsLocationDataSource = nullptr; QVariantMap m_locationProperties;};
#endif // SHOWDEVICELOCATIONUSINGINDOORPOSITIONING_H// [WriteFile Name=ShowDeviceLocationUsingIndoorPositioning, Category=Maps]// [Legal]// 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.// [Legal]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samplesimport QtQuick.Dialogs
Item { function displayFloor(floor) { return floor >= 0 ? floor + 1 : floor; }
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set and keep the focus on MapView to enable keyboard navigation forceActiveFocus(); }
Component.onDestruction: { model.stopLocationDisplay(); } }
Rectangle { id: locationInformationRectangle width: parent.width height: textColumn.height + 20 color: palette.base
ColumnLayout { id: textColumn anchors { top: parent.top left: parent.left margins: 10 }
Label { text: qsTr("Loading indoor data...") visible: model.isLoading }
Label { text: model.locationProperties.floor !== undefined ? qsTr("Current floor: %1").arg(displayFloor(model.locationProperties.floor)) : "" visible: !model.isLoading && model.locationProperties.floor !== undefined } Label { text: model.locationProperties.horizontalAccuracy !== undefined ? qsTr("Accuracy: %1 m").arg(model.locationProperties.horizontalAccuracy.toFixed(2)) : "" visible: !model.isLoading && model.locationProperties.horizontalAccuracy !== undefined } Label { text: qsTr("Data source: %1").arg(model.locationProperties.positionSource !== undefined ? model.locationProperties.positionSource : qsTr("None")) visible: !model.isLoading } Label { text: qsTr("Number of Transmitters: %1").arg(model.locationProperties.transmitterCount) visible: !model.isLoading && model.locationProperties.transmitterCount !== undefined } } }
BusyIndicator { anchors.centerIn: parent running: model.isLoading visible: model.isLoading }
// Declare the C++ instance which creates the map etc. and supply the view ShowDeviceLocationUsingIndoorPositioningSample { id: model mapView: view }
Connections { target: model function onLocationPermissionDenied() { locationPermissionDeniedDialog.open() } }
Dialog { id: locationPermissionDeniedDialog title: qsTr("Location Permission Denied") modal: true standardButtons: Dialog.Ok x: (parent.width - width) / 2 y: (parent.height - height) / 2
Label { text: qsTr("This application requires location permission.") } }
Connections { target: model function onBluetoothPermissionDenied() { bluetoothPermissionDeniedDialog.open() } }
Connections { target: model function onErrorOccurred(errorMessage) { setupErrorLabel.text = errorMessage setupErrorDialog.open() } }
Dialog { id: bluetoothPermissionDeniedDialog title: qsTr("Bluetooth Permission Denied") modal: true standardButtons: Dialog.Ok x: (parent.width - width) / 2 y: (parent.height - height) / 2
Label { text: qsTr("This application requires bluetooth permission.") } }
Dialog { id: setupErrorDialog title: qsTr("Indoor Positioning Error") modal: true standardButtons: Dialog.Ok x: (parent.width - width) / 2 y: (parent.height - height) / 2
Label { id: setupErrorLabel text: "" } }}