Display device location

Learn how to display the current device location on a map or scene.

display device location

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:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

  3. The ArcGIS Maps SDK for Qt, version 200.6.0 or later is installed.

  4. The Qt 6.5.6 software development framework is installed.

Set up authentication

To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication using an ArcGIS Location Platform or an ArcGIS Online account.

Create a new API key access token with privileges to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s):

    • Privileges
      • Location services > Basemaps
  2. Copy and paste the API key access token into a safe location. It will be used in a later step.

Develop or Download

You have two options for completing this tutorial:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

Create a new ArcGIS Maps Qt Creator Project

  1. Start Qt Creator.

  2. In the top menu bar, click File > New Project.

  3. In the New Project dialog, in the left frame, under Projects, select ArcGIS. Then select the ArcGIS Maps 200.6.0 Qt Quick C++ app project template (or a later version) and click Choose. This will launch the template wizard.

  4. In the Project Location template, name your project Display_device_location. You can specify your own "create in" location for where the project will be created or leave the default. Click Next.

  5. In the Define Build System template, select qmake for your build system. Click Next.

  6. In the Define Project Details template, give this app a description or leave as is. Leave the 3D project box unchecked. At the ArcGIS Online Basemap dropdown menu, select Topographic. There is no need to supply an API Key (also called an access token) at this time, you can leave it blank. We will discuss this more in the next section. Click Next.

  7. In the Kit Selection template, check on the kit you previously set up when you installed Qt (Desktop Qt 6.5.6 MSVC2019 64bit or higher required). Click Next.

  8. In the Project Management template, the option to Add as a subproject to root project is only available if you have already created a root project. If you have a version control system set up, you can select it in the dropdown but it is not needed to complete this tutorial. Click Finish to complete the template wizard.

Set developer credentials

To allow your app users to access ArcGIS location services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Projects window, in the Sources folder, open the main.cpp file.

  2. Modify the code to set the accessToken using your API key access token.

    main.cpp
    Expand
    Use dark colors for code blocks
    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
        // 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("");
    
    Expand
  3. Save and close the main.cpp file.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Declare the new method in the header file

  1. 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.h
    Expand
    Use dark colors for code blocks
    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
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void startLocation();
    
    Expand

Include header files in the source code

To create this app you will need to include additional class header files from the ArcGIS Maps Qt C++ API.

  1. In the Projects window, open the Sources folder. Open the display_device_location.cpp file and add the following include statements.

    Display_device_location.cpp
    Use dark colors for code blocks
    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
    #include "Display_device_location.h"
    
    #include "Map.h"
    #include "MapTypes.h"
    #include "MapQuickView.h"
    
    #include "LocationDisplay.h"
    #include "MapViewTypes.h"
    

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.

  1. Add the following method. This code enables LocationDisplay for the map view and assigns a LocationDisplayAutoPanMode that centers the map at the device location.

    Display_device_location.cpp
    Use dark colors for code blocks
    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
    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 setMapView method appearing later in this file gets a handle to the MapView object that was declared in QML code and sets the Map on the MapView for display. This code is installed by the templates that ArcGIS provides when creating a new project in Qt.

  2. Within the setMapView method, add the call to the new method.

    Display_device_location.cpp
    Expand
    Use dark colors for code blocks
    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
    // 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();
    
    Expand
  3. 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.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link under Solution and unzip the file to a location on your machine.

  2. Open the project in Qt Creator.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the set up authentication section.

Set developer credentials in the solution

To allow your app users to access ArcGIS location services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.

  1. In the Projects window, go to the Sources folder

  2. Open the main.cpp file.

  3. Modify the code to set the accessToken using your API key access token.

    main.cpp
    Use dark colors for code blocks
    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
        // 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("");
    
  4. Save and close the file.

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.

Run the app

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:

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