Display your current position on the map, as well as switch between different types of auto pan modes.

Use case
When using a map within a GIS, it may be helpful for a user to know their own location within a map, whether that’s to aid the user’s navigation or to provide an easy means of identifying/collecting geospatial information at their location.
How to use the sample
Tap the button in the lower right (which starts in Stop mode). A menu will appear with the following options:
- Stop - Stops the location display.
- On - Starts the location display with no
AutoPanModemode set. - Re-Center - Starts the location display with auto pan mode set to
LocationDisplayAutoPanMode::Recenter. - Navigation - Starts the location display with auto pan mode set to
LocationDisplayAutoPanMode::Navigation. - Compass - Starts the location display with auto pan mode set to
LocationDisplayAutoPanMode::CompassNavigation.
How it works
- Create a
MapView. - Get the
LocationDisplayobject by callinglocationDisplay()on the map view. - Use
start()andstop()on theLocationDisplayobject as necessary.
Relevant API
- LocationDisplay
- LocationDisplay::setAutoPanMode
- Map
- MapView
Additional information
Location permissions are required for this sample.
Tags
compass, GPS, location, map, mobile, navigation
Sample Code
// [WriteFile Name=DisplayDeviceLocation, Category=Maps]// [Legal]// Copyright 2016 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 "DisplayDeviceLocation.h"
// ArcGIS Maps SDK headers#include "Basemap.h"#include "LocationDisplay.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MapViewTypes.h"
// Qt headers#include <QPermissions>
using namespace Esri::ArcGISRuntime;
const QString DisplayDeviceLocation::s_compassMode = QStringLiteral("Compass");const QString DisplayDeviceLocation::s_navigationMode = QStringLiteral("Navigation");const QString DisplayDeviceLocation::s_recenterMode = QStringLiteral("Re-Center");const QString DisplayDeviceLocation::s_onMode = QStringLiteral("On");const QString DisplayDeviceLocation::s_stopMode = QStringLiteral("Stop");const QString DisplayDeviceLocation::s_closeMode = QStringLiteral("Close");
DisplayDeviceLocation::DisplayDeviceLocation(QQuickItem* parent) : QQuickItem(parent){}
DisplayDeviceLocation::~DisplayDeviceLocation(){ m_mapView->locationDisplay()->stop();}
void DisplayDeviceLocation::init(){ qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<DisplayDeviceLocation>("Esri.Samples", 1, 0, "DisplayDeviceLocationSample");}
void DisplayDeviceLocation::componentComplete(){ QQuickItem::componentComplete();
// find QML MapView component m_mapView = findChild<MapQuickView*>("mapView");
// create a new basemap instance Basemap* basemap = new Basemap(BasemapStyle::ArcGISImageryStandard, this);
// create a new map instance m_map = new Map(basemap, this);
// set map on the map view m_mapView->setMap(m_map);}
void DisplayDeviceLocation::startLocationDisplay(){ QLocationPermission locationPermission{}; locationPermission.setAccuracy(QLocationPermission::Accuracy::Precise); locationPermission.setAvailability(QLocationPermission::Availability::WhenInUse); switch (qApp->checkPermission(locationPermission)) { case Qt::PermissionStatus::Undetermined: qApp->requestPermission(locationPermission, this, &DisplayDeviceLocation::startLocationDisplay); return; case Qt::PermissionStatus::Granted: //! [start location display api snippet] // turn on the location display m_mapView->locationDisplay()->start(); //! [start location display api snippet] return; case Qt::PermissionStatus::Denied: emit locationPermissionDenied(); return; }}
void DisplayDeviceLocation::stopLocationDisplay(){ // stop location display m_mapView->locationDisplay()->stop();}
void DisplayDeviceLocation::setAutoPanMode(QString autoPanMode){ // set the correct auto pan mode on the location display if (autoPanMode == compassMode()) { m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::CompassNavigation); } else if (autoPanMode == navigationMode()) { m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Navigation); } else if (autoPanMode == recenterMode()) { m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Recenter); } else if (autoPanMode == stopMode()) { m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Off); } else if (autoPanMode == onMode()) { m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Off); }}
const QString DisplayDeviceLocation::compassMode(){ return s_compassMode;}
const QString DisplayDeviceLocation::navigationMode(){ return s_navigationMode;}
const QString DisplayDeviceLocation::recenterMode(){ return s_recenterMode;}
const QString DisplayDeviceLocation::onMode(){ return s_onMode;}
const QString DisplayDeviceLocation::stopMode(){ return s_stopMode;}
const QString DisplayDeviceLocation::closeMode(){ return s_closeMode;}// [WriteFile Name=DisplayDeviceLocation, Category=Maps]// [Legal]// Copyright 2016 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 DISPLAY_DEVICE_LOCATION_H#define DISPLAY_DEVICE_LOCATION_H
// Qt headers#include <QQuickItem>
namespace Esri::ArcGISRuntime{ class Map; class MapQuickView;} // namespace Esri::ArcGISRuntime
class DisplayDeviceLocation : public QQuickItem{ Q_OBJECT
Q_PROPERTY(QString compassMode READ compassMode NOTIFY compassModeChanged) Q_PROPERTY(QString navigationMode READ navigationMode NOTIFY navigationModeChanged) Q_PROPERTY(QString recenterMode READ recenterMode NOTIFY recenterModeChanged) Q_PROPERTY(QString onMode READ onMode NOTIFY onModeChanged) Q_PROPERTY(QString stopMode READ stopMode NOTIFY stopModeChanged) Q_PROPERTY(QString closeMode READ closeMode NOTIFY closeModeChanged)
public: explicit DisplayDeviceLocation(QQuickItem* parent = nullptr); ~DisplayDeviceLocation() override;
void componentComplete() override; static void init(); Q_INVOKABLE void startLocationDisplay(); Q_INVOKABLE void stopLocationDisplay(); Q_INVOKABLE void setAutoPanMode(QString autoPanMode);
signals: void compassModeChanged(); void navigationModeChanged(); void recenterModeChanged(); void onModeChanged(); void stopModeChanged(); void closeModeChanged();
void locationPermissionDenied();
private: static const QString compassMode(); static const QString navigationMode(); static const QString recenterMode(); static const QString onMode(); static const QString stopMode(); static const QString closeMode();
private: Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; static const QString s_compassMode; static const QString s_navigationMode; static const QString s_recenterMode; static const QString s_onMode; static const QString s_stopMode; static const QString s_closeMode;};
#endif // DISPLAY_DEVICE_LOCATION_H// [WriteFile Name=DisplayDeviceLocation, Category=Maps]// [Legal]// Copyright 2016 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.Dialogsimport Esri.Samples
DisplayDeviceLocationSample { id: deviceLocationSample width: 800 height: 600
property string currentModeText: deviceLocationSample.stopMode property string currentModeImage: "qrc:/Samples/Maps/DisplayDeviceLocation/Stop.png"
// add a mapView component MapView { anchors.fill: parent objectName: "mapView"
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus();
// populate list model with modes autoPanListModel.append({name: deviceLocationSample.compassMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/Compass.png"}); autoPanListModel.append({name: deviceLocationSample.navigationMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/Navigation.png"}); autoPanListModel.append({name: deviceLocationSample.recenterMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/Re-Center.png"}); autoPanListModel.append({name: deviceLocationSample.onMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/On.png"}); autoPanListModel.append({name: deviceLocationSample.stopMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/Stop.png"}); autoPanListModel.append({name: deviceLocationSample.closeMode, image: "qrc:/Samples/Maps/DisplayDeviceLocation/Close.png"}); } }
Rectangle { id: rect anchors.fill: parent visible: autoPanListView.visible color: "black" opacity: 0.7 }
ListView { id: autoPanListView anchors { right: parent.right bottom: parent.bottom margins: 10 } visible: false width: parent.width height: 300 spacing: 10 model: ListModel { id: autoPanListModel }
delegate: Row { id: autopanRow anchors.right: parent.right spacing: 10
Text { text: name font.pixelSize: 25 color: "white" MouseArea { anchors.fill: parent // When an item in the list view is clicked onClicked: { autopanRow.updateAutoPanMode(); } } }
Image { source: image width: 40 height: width MouseArea { anchors.fill: parent // When an item in the list view is clicked onClicked: { autopanRow.updateAutoPanMode(); } } }
// set the appropriate auto pan mode function updateAutoPanMode() { switch (name) { case deviceLocationSample.compassMode: deviceLocationSample.setAutoPanMode(deviceLocationSample.compassMode); deviceLocationSample.startLocationDisplay(); break; case deviceLocationSample.navigationMode: deviceLocationSample.setAutoPanMode(deviceLocationSample.navigationMode); deviceLocationSample.startLocationDisplay(); break; case deviceLocationSample.recenterMode: deviceLocationSample.setAutoPanMode(deviceLocationSample.recenterMode); deviceLocationSample.startLocationDisplay(); break; case deviceLocationSample.onMode: deviceLocationSample.setAutoPanMode(deviceLocationSample.onMode); deviceLocationSample.startLocationDisplay(); break; case deviceLocationSample.stopMode: deviceLocationSample.stopLocationDisplay(); break; }
if (name !== deviceLocationSample.closeMode) { currentModeText = name; currentModeImage = image; }
// hide the list view currentAction.visible = true; autoPanListView.visible = false; } } }
Row { id: currentAction anchors { right: parent.right bottom: parent.bottom margins: 10 bottomMargin: 25 } spacing: 10
Text { text: currentModeText font.pixelSize: 25 color: "white" MouseArea { anchors.fill: parent onClicked: { currentAction.visible = false; autoPanListView.visible = true; } } }
Image { source: currentModeImage width: 40 height: width MouseArea { anchors.fill: parent onClicked: { currentAction.visible = false; autoPanListView.visible = true; } } } }
Connections { target: deviceLocationSample function onLocationPermissionDenied() { permissionDeniedDialog.open() } }
Dialog { id: permissionDeniedDialog 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 permissions.") } }}// [WriteFile Name=DisplayDeviceLocation, Category=Maps]// [Legal]// Copyright 2015 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]
// sample headers#include "DisplayDeviceLocation.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QCommandLineParser>#include <QDir>#include <QGuiApplication>#include <QQmlEngine>#include <QQuickView>#include <QSettings>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char* argv[]){ QGuiApplication app(argc, argv); app.setApplicationName(QString("DisplayDeviceLocation"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample DisplayDeviceLocation::init();
// Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView);
// Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml"));
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath);
// Set the source view.setSource(QUrl("qrc:/Samples/Maps/DisplayDeviceLocation/DisplayDeviceLocation.qml"));
view.show();
return app.exec();}