Learn how to display the current device location on a map or scene.
You can display the device location on a map or scene. This is important for workflows that require the user's current location, such as finding nearby businesses, navigating from the current location, or identifying and collecting geospatial information.
By default, location display uses the device's location provider. Your app can also process input from other location providers, such as an external GPS receiver or a provider that returns a simulated location. For more information, see the Show device location topic.
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
-
The ArcGIS Runtime API for Qt is installed.
Steps
Create a new ArcGIS Runtime Qt Creator Project
-
Launch Qt Creator and create a new project. Under Choose a Template, select Qt Quick C++ app project for the latest version of ArcGIS Runtime installed.
-
Name your project display_device_location.
-
Accept all defaults. At the Define Project Details window, leave the ArcGIS Online Basemap selection as is. Complete the project creation.
Get an access token
You need an access token to use the location services used in this tutorial.
-
Go to the Create an API key tutorial to obtain an access token using your ArcGIS Location Platform or ArcGIS Online account.
-
Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service.
-
Copy the access token as it will be used in the next step.
To learn more about other ways to get an access token, go to Types of authentication.
Set your API key
-
In the Projects window, in the Sources folder, open the main.cpp file. Modify the code to set the API key to the access token. Save and close the file.
main.cppUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line. // 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("");
Declare the new method in the header file
-
In the Projects window, open the Headers folder. Double-click the file display_device_location.h to open it. Add the new method declaration under
private
. Then save and close the file.: Display_device_location.hUse dark colors for code blocks 40 41 42 43Add line. private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView); void startLocation();
Show the current location
Each map view has its own instance of a
LocationDisplay
for showing the current location (point) of the device. The location is displayed as an overlay in the map view.
-
In the Projects window, open the Sources folder. Open the display_device_location.cpp file and add the new method shown. This code enables
LocationDisplay
for the map view and assigns aLocationDisplayAutoPanMode
that centers the map at the device location.Display_device_location.cppUse dark colors for code blocks 33 34 35 36 37Add line. Add line. Add line. Add line. Add line. Add line. Add line. MapQuickView* Display_device_location::mapView() const { return m_mapView; } void Display_device_location::startLocation() { // start location display m_mapView->locationDisplay()->start(); // center the location display around the device location m_mapView->locationDisplay()->setAutoPanMode(LocationDisplayAutoPanMode::Recenter); }
The
set
method appearing later in this file gets a handle to theMap View Map
object that was declared in QML code and sets theView Map
on theMap
for display. This code is installed by the templates that ArcGIS provides when creating a new project in Qt.View -
Within the
set
method, add the call to the new method.Map View Display_device_location.cppUse dark colors for code blocks 46 47 48 49 50 51 52 53 54 55 56Add line. // Set the view (created in QML) void Display_device_location::setMapView(MapQuickView* mapView) { if (!mapView || mapView == m_mapView) { return; } m_mapView = mapView; m_mapView->setMap(m_map); startLocation();
-
Remove the following include statements; these classes are not used.
Display_device_location.cppUse dark colors for code blocks 19 20 21 22Remove line Remove line #include "Basemap.h" #include "Map.h" #include "MapQuickView.h" #include <QUrl>
-
Press Ctrl + R to run the app.
You should see your current location displayed on the map. Different location symbols are used depending on the auto pan mode and whether a location is acquired. See
LocationDisplayAutoPanMode
for details.
By default, a round blue symbol is used to display the device's location. The location data source tries to get the most accurate location available but depending upon signal strength, satellite positions, and other factors, the location reported could be an approximation. A semi-transparent circle around the location symbol indicates the range of accuracy. As the device moves and location updates are received, the location symbol will be repositioned on the map.
Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:
Not all tutorials listed have instructions for QML.