Display device location with autopan modes

View on GitHubSample viewer app

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

screenshot

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 AutoPanMode mode 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

  1. Create a MapView.
  2. Get the LocationDisplay object by calling locationDisplay() on the map view.
  3. Use start() and stop() on the LocationDisplay object 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

DisplayDeviceLocation.cppDisplayDeviceLocation.cppDisplayDeviceLocation.hDisplayDeviceLocation.qml
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
// [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"

#if (QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)) || defined(Q_OS_IOS) || defined(Q_OS_MACOS) || defined(Q_OS_ANDROID)
#define PERMISSIONS_PLATFORM
#include <QPermissions>
#endif

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()
{
  // https://bugreports.qt.io/browse/QTBUG-116178
#ifdef PERMISSIONS_PLATFORM
  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;
  }
#else
  m_mapView->locationDisplay()->start();
#endif
}

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;
}

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