Skip to content

Learn how to download and display an offline map for a user-defined geographical area of a web map.

display an offline map on demand

Offline maps allow users to continue working when network connectivity is poor or lost. If a web map is enabled for offline use, a user can request that ArcGIS generates an offline map for a specified geographic area of interest.

In this tutorial, you will download an offline map for an area of interest from the web map of the stormwater network within Naperville, IL, USA . You can then use this offline map without a network connection.

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.8.0 or later is installed.

  4. The Qt 6.8.2 software development framework or later is installed.

Get the web map item ID

You can use ArcGIS tools to create and view web maps. Use the Map Viewer to identify the web map item ID. This item ID will be used later in the tutorial.

  1. Go to the Naperville water network in the Map Viewer in ArcGIS Online. This web map displays stormwater network within Naperville, IL, USA .
  2. Make a note of the item ID at the end of the browser's URL. The item ID should be:

    acc027394bc84c2fb04d1ed317aac674

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

Open a Qt Creator project

  1. Open the project you created by completing the Display a map tutorial.

  2. Continue with the following instructions to display an offline map.

Update the header file

You can display a web map using the web map's item ID. Create a map from the web map portal item, and display it in your app.

  1. In Projects, double-click Headers > Display_a_map.h to open the file. Add the following classes to the header file.

    Display_a_map.h
    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
    namespace Esri::ArcGISRuntime {
    
    class Map;
    
    class MapQuickView;
    
    class PortalItem;
    class OfflineMapTask;
    
  2. Remove the unnecessary declaration for the setViewpoint() function.

    Display_a_map.h
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void setupViewpoint();
    
        Esri::ArcGISRuntime::Map* m_map = nullptr;
    
        Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    
    };
  3. Add the generateMapByExtent() function declaration and member variables m_portalItem and m_offlineMapTask. Then save the file.

    Display_a_map.h
    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
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void generateMapByExtent();
    
        Esri::ArcGISRuntime::Map* m_map = nullptr;
    
        Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    
        Esri::ArcGISRuntime::PortalItem* m_portalItem = nullptr;
        Esri::ArcGISRuntime::OfflineMapTask* m_offlineMapTask = nullptr;
    

Update include statements

  1. In the Projects window, open the Sources folder and then open the Display_a_map.cpp file. Remove the following unneeded include statements.

    Display_a_map.cpp
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    #include "Display_a_map.h"
    #include "Map.h"
    
    #include "MapTypes.h"
    
    #include "MapQuickView.h"
    
    #include "Point.h"
    #include "Viewpoint.h"
    
    #include "SpatialReference.h"
    #include <QFuture>
  2. Add the following new include statements.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    #include "Display_a_map.h"
    #include "Map.h"
    
    #include "MapQuickView.h"
    
    #include "SpatialReference.h"
    #include <QFuture>
    
    #include "Envelope.h"
    #include "EnvelopeBuilder.h"
    #include "Error.h"
    #include "GenerateOfflineMapParameters.h"
    #include "GenerateOfflineMapJob.h"
    #include "GenerateOfflineMapResult.h"
    #include "Graphic.h"
    #include "GraphicListModel.h"
    #include "GraphicsOverlay.h"
    #include "GraphicsOverlayListModel.h"
    #include "OfflineMapTask.h"
    #include "Portal.h"
    #include "PortalItem.h"
    #include "SimpleFillSymbol.h"
    #include "SimpleLineSymbol.h"
    #include "SymbolTypes.h"
    #include "TaskTypes.h"
    #include <QDir>
    #include <QUuid>
    

Remove remaining unneeded code

At this point we will remove the additional unnecessary code before we add the logic for the rest of the app.

  1. Continuing on the Display_a_map.cpp file, remove the setupViewpoint() method.

    Display_a_map.cpp
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    void Display_a_map::setupViewpoint()
    {
    
        const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
        const Viewpoint viewpoint(center, 100000.0);
        m_mapView->setViewpointAsync(viewpoint);
    
    }
  2. Remove the call to the setupViewpoint() method from within the setMapView(MapQuickView* mapView) function.

    Display_a_map.cpp
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
        if (!mapView || mapView == m_mapView)
        {
            return;
        }
    
        m_mapView = mapView;
    
        m_mapView->setMap(m_map);
    
        setupViewpoint();
    
        emit mapViewChanged();
    }

Update the constructor

At this point you will update the constructor in the app.

  1. Still editing the Display_a_map.cpp file, remove the comma after QObject(parent) and then modify the constructor to remove initialization with BasemapStyle and the Map.

    Display_a_map.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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
    
        QObject(parent),
    
        m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    
  2. Inside the body of the constructor, add code to Instantiate a Portal and from that, a PortalItem using the web map item ID for the Naperville water network. Use that to create a new Map. Then create a new OfflineMapTask referencing that portal item.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
    
        QObject(parent)
    
    {
    
        Portal* portal = new Portal(false, this);
        m_portalItem = new PortalItem(portal, "acc027394bc84c2fb04d1ed317aac674", this);
        m_map = new Map(m_portalItem, this);
        m_offlineMapTask = new OfflineMapTask(m_portalItem, this);
    

Specify the area of the web map to take offline

  1. Continuing on the Display_a_map.cpp file, add the call to generateMapByExtent() method within the setMapView(MapQuickView* mapView) function. The next step implements this method.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    // Set the view (created in QML)
    void Display_a_map::setMapView(MapQuickView* mapView)
    {
        if (!mapView || mapView == m_mapView)
        {
            return;
        }
    
        m_mapView = mapView;
    
        m_mapView->setMap(m_map);
    
        generateMapByExtent();
    
        emit mapViewChanged();
    }
    
  2. Begin to implement the generateMapByExtent() function. Use EnvelopeBuilder to set four parameters and then call toEnvelope() to create the envelope. Add this new function near the bottom of the file.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    void Display_a_map::generateMapByExtent()
    {
        // Create envelope to define area of interest
        EnvelopeBuilder* envelopeBuilder = new EnvelopeBuilder(SpatialReference::wgs84(), this);
    
        envelopeBuilder->setXMin(-88.1526);
        envelopeBuilder->setXMax(-88.1490);
        envelopeBuilder->setYMin(41.7694);
        envelopeBuilder->setYMax(41.7714);
    
        Envelope offlineArea = envelopeBuilder->toEnvelope();
    
    }
  3. Add a graphic to generateMapByExtent() to show the area you will take offline.

    Create a Graphic using the envelope, SimpleFillSymbol and SimpleLineSymbol. From this create a GraphicsOverlay and add it to the mapView.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    void Display_a_map::generateMapByExtent()
    {
        // Create envelope to define area of interest
        EnvelopeBuilder* envelopeBuilder = new EnvelopeBuilder(SpatialReference::wgs84(), this);
    
        envelopeBuilder->setXMin(-88.1526);
        envelopeBuilder->setXMax(-88.1490);
        envelopeBuilder->setYMin(41.7694);
        envelopeBuilder->setYMax(41.7714);
    
        Envelope offlineArea = envelopeBuilder->toEnvelope();
    
        Graphic* box = new Graphic(offlineArea, new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, Qt::transparent, new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::red, 3, this), this), this);
        GraphicsOverlay* boxOverlay = new GraphicsOverlay(this);
        boxOverlay->graphics()->append(box);
    
        // add graphics overlay to the map view
        m_mapView->graphicsOverlays()->append(boxOverlay);
    
    }

Download and display the offline map

  1. Add the following createDefaultGenerateOfflineMapParametersAsync method that creates a GenerateOfflineMapParameters object. These offline map parameters will serve as the input to create a GenerateOfflineMapJob. Store the resulting offline mobile map package (MMPK) in a location on your development machine. It is important that you specify the folder location that exists on the device and that it matches the code for the outputPath variable.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    void Display_a_map::generateMapByExtent()
    {
        // Create envelope to define area of interest
        EnvelopeBuilder* envelopeBuilder = new EnvelopeBuilder(SpatialReference::wgs84(), this);
    
        envelopeBuilder->setXMin(-88.1526);
        envelopeBuilder->setXMax(-88.1490);
        envelopeBuilder->setYMin(41.7694);
        envelopeBuilder->setYMax(41.7714);
    
        Envelope offlineArea = envelopeBuilder->toEnvelope();
    
        Graphic* box = new Graphic(offlineArea, new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, Qt::transparent, new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::red, 3, this), this), this);
        GraphicsOverlay* boxOverlay = new GraphicsOverlay(this);
        boxOverlay->graphics()->append(box);
    
        // add graphics overlay to the map view
        m_mapView->graphicsOverlays()->append(boxOverlay);
    
        // generate the offline map parameters
        m_offlineMapTask->createDefaultGenerateOfflineMapParametersAsync(offlineArea).then(this,[this](const GenerateOfflineMapParameters& params)
        {
    
            // Generate the offline map. Store the .mmpk in the desired location.
            const QString outputPath = "ENTER_PATH_TO_MMPK_FILE/offlinemap.mmpk"; // Ex: "C:/mmpk_folder/offlinemap.mmpk"
    
            GenerateOfflineMapJob* generateJob = m_offlineMapTask->generateOfflineMap(params, outputPath);
            if (!generateJob)
                return;
    
    }
  2. Add a connect that monitors the status of the GenerateOfflineMapJob and returns changes to status.

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    void Display_a_map::generateMapByExtent()
    {
        // Create envelope to define area of interest
        EnvelopeBuilder* envelopeBuilder = new EnvelopeBuilder(SpatialReference::wgs84(), this);
    
        envelopeBuilder->setXMin(-88.1526);
        envelopeBuilder->setXMax(-88.1490);
        envelopeBuilder->setYMin(41.7694);
        envelopeBuilder->setYMax(41.7714);
    
        Envelope offlineArea = envelopeBuilder->toEnvelope();
    
        Graphic* box = new Graphic(offlineArea, new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, Qt::transparent, new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::red, 3, this), this), this);
        GraphicsOverlay* boxOverlay = new GraphicsOverlay(this);
        boxOverlay->graphics()->append(box);
    
        // add graphics overlay to the map view
        m_mapView->graphicsOverlays()->append(boxOverlay);
    
        // generate the offline map parameters
        m_offlineMapTask->createDefaultGenerateOfflineMapParametersAsync(offlineArea).then(this,[this](const GenerateOfflineMapParameters& params)
        {
    
            // Generate the offline map. Store the .mmpk in the desired location.
            const QString outputPath = "ENTER_PATH_TO_MMPK_FILE/offlinemap.mmpk"; // Ex: "C:/mmpk_folder/offlinemap.mmpk"
    
            GenerateOfflineMapJob* generateJob = m_offlineMapTask->generateOfflineMap(params, outputPath);
            if (!generateJob)
                return;
    
            // use connect to monitor job status for changes and return job status
            connect(generateJob, &GenerateOfflineMapJob::statusChanged, this, [this, generateJob]()
                    {
                        if (generateJob->jobStatus() == JobStatus::Succeeded)
                        {
                            qDebug() << "generating offline map";
                            m_mapView->setMap(generateJob->result()->offlineMap(this));
    
                            qDebug() << (generateJob->error().isEmpty() ? "No errors" : (generateJob->error().message() + generateJob->error().additionalMessage()));
                        }
    
                        else if (generateJob->jobStatus() == JobStatus::Failed)
                        {
                            qWarning() << generateJob->error().message() << generateJob->error().additionalMessage();
                        }
                    });
    
    }
  3. Finally, add another connect that displays progress of the GenerateOfflineMapJob as the job executes. Then add code for the generateJob->start().

    Display_a_map.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
    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
    166
    167
    168
    169
    170
    void Display_a_map::generateMapByExtent()
    {
        // Create envelope to define area of interest
        EnvelopeBuilder* envelopeBuilder = new EnvelopeBuilder(SpatialReference::wgs84(), this);
    
        envelopeBuilder->setXMin(-88.1526);
        envelopeBuilder->setXMax(-88.1490);
        envelopeBuilder->setYMin(41.7694);
        envelopeBuilder->setYMax(41.7714);
    
        Envelope offlineArea = envelopeBuilder->toEnvelope();
    
        Graphic* box = new Graphic(offlineArea, new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, Qt::transparent, new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, Qt::red, 3, this), this), this);
        GraphicsOverlay* boxOverlay = new GraphicsOverlay(this);
        boxOverlay->graphics()->append(box);
    
        // add graphics overlay to the map view
        m_mapView->graphicsOverlays()->append(boxOverlay);
    
        // generate the offline map parameters
        m_offlineMapTask->createDefaultGenerateOfflineMapParametersAsync(offlineArea).then(this,[this](const GenerateOfflineMapParameters& params)
        {
    
            // Generate the offline map. Store the .mmpk in the desired location.
            const QString outputPath = "ENTER_PATH_TO_MMPK_FILE/offlinemap.mmpk"; // Ex: "C:/mmpk_folder/offlinemap.mmpk"
    
            GenerateOfflineMapJob* generateJob = m_offlineMapTask->generateOfflineMap(params, outputPath);
            if (!generateJob)
                return;
    
            // use connect to monitor job status for changes and return job status
            connect(generateJob, &GenerateOfflineMapJob::statusChanged, this, [this, generateJob]()
                    {
                        if (generateJob->jobStatus() == JobStatus::Succeeded)
                        {
                            qDebug() << "generating offline map";
                            m_mapView->setMap(generateJob->result()->offlineMap(this));
    
                            qDebug() << (generateJob->error().isEmpty() ? "No errors" : (generateJob->error().message() + generateJob->error().additionalMessage()));
                        }
    
                        else if (generateJob->jobStatus() == JobStatus::Failed)
                        {
                            qWarning() << generateJob->error().message() << generateJob->error().additionalMessage();
                        }
                    });
    
            // use connect to monitor job progress changes and return progress return job progress
            connect(generateJob, &GenerateOfflineMapJob::progressChanged, this, [generateJob]()
                    {
                        qDebug() << "Job status:" << generateJob->progress() << "%";
                    });
    
            generateJob->start();
        });
    
    }

Set developer credentials

For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.

Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.

Set the API Key

  1. In the project Sources folder of Qt Creator, open the main.cpp file.

  2. Modify the code to set the accessToken using your API key access token (highlighted in yellow).

    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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
        // 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
        {
            ArcGISRuntimeEnvironment::setApiKey(accessToken);
        }
    
  3. Save 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.

Press Ctrl + R to run the app.

Initially, you should see the map of the stormwater network for Naperville, IL, USA, with a red outline as before. At the Application Output tab in Creator, the Job status percentage should increment up to 100%, and then you should see the offline map for the specified area of the stormwater network for Naperville, IL, USA. You should also be able to see the downloaded .mmpk file on the device hard drive for the outputPath variable you specified in the code. Remove your network connection and you will still be able to use the mouse to drag, scroll, and double-click the map view to explore this offline 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 .pro project file in Qt Creator.

Since the downloaded solution does not contain authentication credentials, you must set up authentication to create the developer credentials and add them to the project.

In the project Sources folder of Qt Creator, open the Display_a_map.cpp file.

You will need to store the resulting offline mobile map package (MMPK) in a location on your development machine. It is important that you specify the folder location that exists on the device and that it matches the code for the outputPath variable. The code line you need to modify is found in the generateMapByExtent() method and is highlighted in yellow.

Display_a_map.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
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
166
167
168
169
170
    // generate the offline map parameters
    m_offlineMapTask->createDefaultGenerateOfflineMapParametersAsync(offlineArea).then(this,[this](const GenerateOfflineMapParameters& params)
    {

        // Generate the offline map. Store the .mmpk in the desired location.
        const QString outputPath = "ENTER_PATH_TO_MMPK_FILE/offlinemap.mmpk"; // Ex: "C:/mmpk_folder/offlinemap.mmpk"

        GenerateOfflineMapJob* generateJob = m_offlineMapTask->generateOfflineMap(params, outputPath);
        if (!generateJob)
            return;

For the final steps of this tutorial, click the tab below that corresponds to the authentication type (API key authentication or User authentication) that you chose when you completed the Display a map tutorial.

Be sure to also provide the same authentication (API key or user authentication Client ID/Redirect URL) that you used for the Display a map tutorial.

Set developer credentials in the solution

Set the API Key

  1. In the project Sources folder of Qt Creator, open the main.cpp file.

  2. Modify the code to set the accessToken using your API key access token (highlighted in yellow).

    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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
        // 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
        {
            ArcGISRuntimeEnvironment::setApiKey(accessToken);
        }
    
  3. Save 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.

Run the app

Press Ctrl + R to run the app.

Initially, you should see the map of the stormwater network for Naperville, IL, USA, with a red outline as before. At the Application Output tab in Creator, the Job status percentage should increment up to 100%, and then you should see the offline map for the specified area of the stormwater network for Naperville, IL, USA. You should also be able to see the downloaded .mmpk file on the device hard drive for the outputPath variable you specified in the code. Remove your network connection and you will still be able to use the mouse to drag, scroll, and double-click the map view to explore this offline map.

What's next?

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.