Display a map from a mobile map package

Learn how to display a map from a mobile map package (MMPK).

display a map from a mobile map package

In this tutorial you will display a fully interactive map from a mobile map package (MMPK). The map contains a basemap layer and data layers and does not require a network connection.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.
  3. The ArcGIS Runtime API for Qt is installed.

Steps

Add a mobile map package

  1. Download or create the MahouRivieraTrails.mmpk mobile map package. You can download the mobile map package from here: MahouRivieraTrails.mmpk. Otherwise, complete the steps in the Create a mobile map package tutorial, using ArcGIS Pro to create the package.

  2. Copy the file MahouRivieraTrails.mmpk to the following local path: <userhome>/ArcGIS/Runtime/Data/mmpk/, where <userhome> is your user home location (for example, C:\users\username on Windows, or $HOME on Linux).

Create a new ArcGIS Runtime Qt Creator Project

  1. 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.

  2. Name your project display_an_mmpk.

  3. Accept all defaults. At the Define Project Details window, leave the ArcGIS Online Basemap selection as is. Complete the project creation.

Modify the project to use a mobile map package

The project you created from the template includes classes for the Basemap, Map, and QUrl. Because the mobile map package contains the map, basemap, and all the data layers it requires, in the following steps you will remove the unneeded code from your project and add/modify other code as needed.

  1. In Projects, double-click Headers > Display_an_mmpk.h to open the file. Remove the code that forward declares the Map class.

    Display_an_mmpk.h
    17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23
    Remove line
    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
    //   Copyright 2020 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
    //
    //   https://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.
    //
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    
    class MapQuickView;
    }
    }
    
    #include <QObject>
    
    class Display_a_map : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
    public:
        explicit Display_a_map(QObject* parent = nullptr);
        ~Display_a_map() override;
    
    signals:
        void mapViewChanged();
    
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void setupMap();
    
    
        Esri::ArcGISRuntime::Map* m_map = nullptr;
    
        Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    };
    
    #endif // DISPLAY_A_MAP_H
  2. Remove the code that creates the Map pointer variable m_map.

    This code was generated by the template, but in this tutorial the Map class is not needed because it is part of the MMPK.

    Display_an_mmpk.h
    43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 44 45 46 47 47 47 48 49 49 49 49 49 49
    Remove line
    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
    //   Copyright 2020 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
    //
    //   https://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.
    //
    
    #ifndef DISPLAY_A_MAP_H
    #define DISPLAY_A_MAP_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class Map;
    
    class MapQuickView;
    }
    }
    
    #include <QObject>
    
    class Display_a_map : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
    public:
        explicit Display_a_map(QObject* parent = nullptr);
        ~Display_a_map() override;
    
    signals:
        void mapViewChanged();
    
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void setupMap();
    
    
        Esri::ArcGISRuntime::Map* m_map = nullptr;
    
        Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    };
    
    #endif // DISPLAY_A_MAP_H
  3. Under private, declare the new function you will implement to load the mobile map package. Then save and close the file.

    Display_an_mmpk.h
    39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 40 41 42 43 44 44 44 44 44 44
    Add line.
    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
    // Copyright 2021 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the Sample code usage restrictions document for further information.
    //
    
    #ifndef DISPLAY_AN_MMPK_H
    #define DISPLAY_AN_MMPK_H
    
    namespace Esri
    {
    namespace ArcGISRuntime
    {
    
    class MapQuickView;
    }
    }
    
    #include <QObject>
    
    class Display_an_mmpk : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged)
    public:
        explicit Display_an_mmpk(QObject* parent = nullptr);
        ~Display_an_mmpk() override;
    
    signals:
        void mapViewChanged();
    
    private:
        Esri::ArcGISRuntime::MapQuickView* mapView() const;
        void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
    
        void setupMapFromMmpk();
    
        Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
    };
    
    #endif // DISPLAY_AN_MMPK_H
  4. In Projects, double-click Sources > Display_an_mmpk.cpp to open the file. Remove the lines #include "Basemap.h" and #include "Map.h". Also remove #include <QUrl> because it is not needed.

    This code was generated by the template, but map and basemap are not needed for this tutorial because they are part of the MMPK.

    Display_an_mmpk.cpp
    14 14 14 14 14 14 14 14 14 14 14 14 14 14 15 16 17 18 19 20 21 22 23 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
    Remove lineRemove lineRemove line
    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
    //   Copyright 2020 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
    //
    //   https://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.
    //
    
    #include "Display_a_map.h"
    
    #include "ArcGISRuntimeEnvironment.h"
    
    #include "Basemap.h"
    #include "Map.h"
    
    #include "MapQuickView.h"
    
    #include <QUrl>
    
    
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
    
        QObject(parent),
    
        m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    
    {
    }
    
    Display_a_map::~Display_a_map()
    {
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
        return m_mapView;
    }
    
    void Display_a_map::setupMap()
    {
    
      const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
      const Viewpoint viewpoint(center, 100000.0);
      m_mapView->setViewpoint(viewpoint);
    
    }
    
    // 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);
    
        setupMap();
    
        emit mapViewChanged();
    }
    
  5. Add include statements for the MobileMapPackage class, the Error class, and the Qt Qdir class.

    Display_an_mmpk.cpp
    12 12 12 12 12 12 12 12 12 12 12 12 13 14 15 16 17 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
    Add line.Add line.Add line.
    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
    // Copyright 2021 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the Sample code usage restrictions document for further information.
    //
    
    #include "Display_an_mmpk.h"
    
    #include "MapQuickView.h"
    
    #include "MobileMapPackage.h"
    #include "Error.h"
    #include <QDir>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_an_mmpk::Display_an_mmpk(QObject* parent /* = nullptr */):
        QObject(parent)
    {
    }
    
    Display_an_mmpk::~Display_an_mmpk()
    {
    }
    
    MapQuickView* Display_an_mmpk::mapView() const
    {
        return m_mapView;
    }
    
    // Set the view (created in QML)
    void Display_an_mmpk::setMapView(MapQuickView* mapView)
    {
        if (!mapView || mapView == m_mapView)
        {
            return;
        }
    
        m_mapView = mapView;
    
        setupMapFromMmpk();
    
        emit mapViewChanged();
    
    }
    
    void Display_an_mmpk::setupMapFromMmpk()
    {
        // Instantiate a MobileMapPackage object and establish the path to the MMPK file.
        MobileMapPackage* m_mobileMapPackage = new MobileMapPackage(QDir::homePath() + "/ArcGIS/Runtime/Data/mmpk/MahouRivieraTrails.mmpk", this);
    
        // Use connect to signal when the package is done loading so that m_mapView can be set to the first map (0).
        // Check that the mmpk file has loaded correctly.
        connect(m_mobileMapPackage, &MobileMapPackage::doneLoading, this, [m_mobileMapPackage, this](Error error)
        {
            // Check that the mmpk file has loaded correctly
            if (!error.isEmpty())
            {
                qDebug() << "Error:" << error.message()<< error.additionalMessage();
                return;
            }
    
            // Get the first map in the list of maps, and set the map on the map view to display.
            // This could be set to any map in the list.
            m_mapView->setMap(m_mobileMapPackage->maps().at(0));
        });
    
        m_mobileMapPackage->load();
    }
    
  6. Remove the comma after QObject(parent) and then modify the constructor to remove initialization with BasemapStyle and the Map. (Note that the BasemapStyle configured for your project may be different than that shown here.)

    Display_an_mmpk.cpp
    29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 30 31 32 33 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34
    Change lineRemove line
    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
    //   Copyright 2020 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
    //
    //   https://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.
    //
    
    #include "Display_a_map.h"
    
    #include "ArcGISRuntimeEnvironment.h"
    
    #include "Basemap.h"
    #include "Map.h"
    
    #include "MapQuickView.h"
    
    #include <QUrl>
    
    
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
    
        QObject(parent),
    
        m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    
    {
    }
    
    Display_a_map::~Display_a_map()
    {
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
        return m_mapView;
    }
    
    void Display_a_map::setupMap()
    {
    
      const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
      const Viewpoint viewpoint(center, 100000.0);
      m_mapView->setViewpoint(viewpoint);
    
    }
    
    // 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);
    
        setupMap();
    
        emit mapViewChanged();
    }
    
  7. Remove the line of code that assigns the Map (m_map) to m_mapView.

    This code was generated by the template, but is not needed because the map is provided by the MMPK.

    Display_an_mmpk.cpp
    56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 57 58 59 60 61 62 63 64 65 66 67 67 67 67 67 67
    Remove line
    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
    //   Copyright 2020 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
    //
    //   https://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.
    //
    
    #include "Display_a_map.h"
    
    #include "ArcGISRuntimeEnvironment.h"
    
    #include "Basemap.h"
    #include "Map.h"
    
    #include "MapQuickView.h"
    
    #include <QUrl>
    
    
    
    using namespace Esri::ArcGISRuntime;
    
    Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
    
        QObject(parent),
    
        m_map(new Map(BasemapStyle::ArcGISTopographic, this))
    
    {
    }
    
    Display_a_map::~Display_a_map()
    {
    }
    
    MapQuickView* Display_a_map::mapView() const
    {
        return m_mapView;
    }
    
    void Display_a_map::setupMap()
    {
    
      const Point center(-118.80543, 34.02700, SpatialReference::wgs84());
      const Viewpoint viewpoint(center, 100000.0);
      m_mapView->setViewpoint(viewpoint);
    
    }
    
    // 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);
    
        setupMap();
    
        emit mapViewChanged();
    }
    
  8. Add a call to a function that reads the map from the mobile map package. You will implement this function in the next step.

    Display_an_mmpk.cpp
    36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 37 38 39 40 41 42 43 44 45 46 47 48 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49
    Add line.
    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
    // Copyright 2021 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the Sample code usage restrictions document for further information.
    //
    
    #include "Display_an_mmpk.h"
    
    #include "MapQuickView.h"
    
    #include "MobileMapPackage.h"
    #include "Error.h"
    #include <QDir>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_an_mmpk::Display_an_mmpk(QObject* parent /* = nullptr */):
        QObject(parent)
    {
    }
    
    Display_an_mmpk::~Display_an_mmpk()
    {
    }
    
    MapQuickView* Display_an_mmpk::mapView() const
    {
        return m_mapView;
    }
    
    // Set the view (created in QML)
    void Display_an_mmpk::setMapView(MapQuickView* mapView)
    {
        if (!mapView || mapView == m_mapView)
        {
            return;
        }
    
        m_mapView = mapView;
    
        setupMapFromMmpk();
    
        emit mapViewChanged();
    
    }
    
    void Display_an_mmpk::setupMapFromMmpk()
    {
        // Instantiate a MobileMapPackage object and establish the path to the MMPK file.
        MobileMapPackage* m_mobileMapPackage = new MobileMapPackage(QDir::homePath() + "/ArcGIS/Runtime/Data/mmpk/MahouRivieraTrails.mmpk", this);
    
        // Use connect to signal when the package is done loading so that m_mapView can be set to the first map (0).
        // Check that the mmpk file has loaded correctly.
        connect(m_mobileMapPackage, &MobileMapPackage::doneLoading, this, [m_mobileMapPackage, this](Error error)
        {
            // Check that the mmpk file has loaded correctly
            if (!error.isEmpty())
            {
                qDebug() << "Error:" << error.message()<< error.additionalMessage();
                return;
            }
    
            // Get the first map in the list of maps, and set the map on the map view to display.
            // This could be set to any map in the list.
            m_mapView->setMap(m_mobileMapPackage->maps().at(0));
        });
    
        m_mobileMapPackage->load();
    }
    
  9. Add code to implement setupMapFromMmpk(). This function defines the path to the MMPK file, instantiates the mobile map package using the MobileMapPackage constructor, loads the mobile map package, and once loaded, sets the first map in the mobile map package to the MapView. This also checks that the MMPK file loaded correctly.

    Display_an_mmpk.cpp
    46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 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
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    // Copyright 2021 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the Sample code usage restrictions document for further information.
    //
    
    #include "Display_an_mmpk.h"
    
    #include "MapQuickView.h"
    
    #include "MobileMapPackage.h"
    #include "Error.h"
    #include <QDir>
    
    using namespace Esri::ArcGISRuntime;
    
    Display_an_mmpk::Display_an_mmpk(QObject* parent /* = nullptr */):
        QObject(parent)
    {
    }
    
    Display_an_mmpk::~Display_an_mmpk()
    {
    }
    
    MapQuickView* Display_an_mmpk::mapView() const
    {
        return m_mapView;
    }
    
    // Set the view (created in QML)
    void Display_an_mmpk::setMapView(MapQuickView* mapView)
    {
        if (!mapView || mapView == m_mapView)
        {
            return;
        }
    
        m_mapView = mapView;
    
        setupMapFromMmpk();
    
        emit mapViewChanged();
    
    }
    
    void Display_an_mmpk::setupMapFromMmpk()
    {
        // Instantiate a MobileMapPackage object and establish the path to the MMPK file.
        MobileMapPackage* m_mobileMapPackage = new MobileMapPackage(QDir::homePath() + "/ArcGIS/Runtime/Data/mmpk/MahouRivieraTrails.mmpk", this);
    
        // Use connect to signal when the package is done loading so that m_mapView can be set to the first map (0).
        // Check that the mmpk file has loaded correctly.
        connect(m_mobileMapPackage, &MobileMapPackage::doneLoading, this, [m_mobileMapPackage, this](Error error)
        {
            // Check that the mmpk file has loaded correctly
            if (!error.isEmpty())
            {
                qDebug() << "Error:" << error.message()<< error.additionalMessage();
                return;
            }
    
            // Get the first map in the list of maps, and set the map on the map view to display.
            // This could be set to any map in the list.
            m_mapView->setMap(m_mobileMapPackage->maps().at(0));
        });
    
        m_mobileMapPackage->load();
    }
    

    A MobileMapPackage can contain many maps in a Maps list. Loading the mobile map package is an asynchronous process, and ArcGIS Runtime and Qt take care of reading the file on a thread that will not block the UI.

  10. Press <Ctrl+R> to run the app.

You see a map of trailheads, trails, and parks for the area south of the Santa Monica mountains. Drag, zoom in, and zoom out to explore the map.

What's next?

Learn how to use additional API features, ArcGIS platform 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.