Display device location
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
The following are required for this tutorial:
- An ArcGIS account to access API keys. If you don't have an account, sign up for free.
- Your system meets the system requirements.
- The ArcGIS Maps SDK for Qt, version 200.2.0 or later is installed.
- The Qt 6.5.1 software development framework is installed.
Steps
Create a new ArcGIS Maps Qt Creator Project
Start Qt Creator.
Click File > New File or Project. Under Projects, select ArcGIS.
Select the ArcGIS Maps 200.2.0 Qt Quick C++ app project template (or a later version) and click Choose.
You may have several selections for the ArcGIS project type. Be sure to select ArcGIS Maps 200.2.0 Qt Quick C++ app (or a later version).
In the Project Location dialog, name your project display_device_location. Click Next.
In the Define Build System dialog, select qmake for your build system. Click Next.
In the Define Project Details dialog, give this app a description or leave it as is. Leave the rest of this dialog as is.
Leave the 3D project box unchecked. At the ArcGIS Online Basemap dropdown menu, select Streets. Then click Next.
On the Kit Selection dialog, check the kit(s) you previously set up when you installed the API. You should select a Desktop kit to run this tutorial. Then click Next.
At the Project Management dialog, the option to Add as a subproject to root project is only available if you have already created a root project. Ignore this dialog for this tutorial. Click Next.
Set your API key
An API key is required to enable access to services, web maps, and web scenes hosted in ArcGIS Online.If you haven't already, go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.
In the Projects window, in the Sources folder, open the main.cpp file. Modify the code to set the API key. Paste the API key, acquired from your dashboard, between the quotes. Then save and close the file.
main.cppUse dark colors for code blocks Add line. Add line. Add line. Add line. Add line.
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 Add line.
Include header files in the source code
To create this app you will need to include addtional class header files from the ArcGIS Maps Qt C++ API.
- In the Projects window, open the Sources folder. Open the display_device_location.cpp file and add the following include statements.
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.
- Add the following method. This code enables
LocationDisplay
for the map view and assigns aLocationDisplayAutoPanMode
that centers the map at the device location.
The set
method appearing later in this file gets a handle to the Map
object that was declared in QML code and sets the Map
on the Map
for display. This code is installed by the templates that ArcGIS provides when creating a new project in Qt.
Within the
set
method, add the call to the new method.M a p View Display_device_location.cppUse dark colors for code blocks Add line. 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.