Learn how to display a map from a mobile map package (MMPK).
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:
An ArcGIS account to access API keys. If you don't have an account, sign up for free.
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.
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
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_an_mmpk.
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.
In Projects, double-click Headers > Display_an_mmpk.h to open the file. Remove the code that forward declares the Map class.
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_Hnamespace Esri
{
namespace ArcGISRuntime
{
classMap;classMapQuickView;}
}
#include<QObject>classDisplay_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:
voidmapViewChanged();
private:
Esri::ArcGISRuntime::MapQuickView* mapView()const;
voidsetMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
voidsetupMap();
Esri::ArcGISRuntime::Map* m_map = nullptr;
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
};
#endif// DISPLAY_A_MAP_H
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.
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_Hnamespace Esri
{
namespace ArcGISRuntime
{
classMap;classMapQuickView;}
}
#include<QObject>classDisplay_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:
voidmapViewChanged();
private:
Esri::ArcGISRuntime::MapQuickView* mapView()const;
voidsetMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
voidsetupMap();
Esri::ArcGISRuntime::Map* m_map = nullptr;
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
};
#endif// DISPLAY_A_MAP_H
Under private, declare the new function you will implement to load the mobile map package. Then save and close the file.
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_Hnamespace Esri
{
namespace ArcGISRuntime
{
classMapQuickView;}
}
#include<QObject>classDisplay_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:
voidmapViewChanged();
private:
Esri::ArcGISRuntime::MapQuickView* mapView()const;
voidsetMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
voidsetupMapFromMmpk();
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
};
#endif// DISPLAY_AN_MMPK_H
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.
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>usingnamespace Esri::ArcGISRuntime;
Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
QObject(parent),
m_map(newMap(BasemapStyle::ArcGISTopographic, this))
{
}
Display_a_map::~Display_a_map()
{
}
MapQuickView* Display_a_map::mapView()const{
return m_mapView;
}
voidDisplay_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)voidDisplay_a_map::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
m_mapView->setMap(m_map);
setupMap();
emit mapViewChanged();
}
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>usingnamespace 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)voidDisplay_an_mmpk::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
setupMapFromMmpk();
emit mapViewChanged();
}
voidDisplay_an_mmpk::setupMapFromMmpk(){
// Instantiate a MobileMapPackage object and establish the path to the MMPK file. MobileMapPackage* m_mobileMapPackage = newMobileMapPackage(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 correctlyif (!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();
}
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.)
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>usingnamespace Esri::ArcGISRuntime;
Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
QObject(parent),
m_map(newMap(BasemapStyle::ArcGISTopographic, this))
{
}
Display_a_map::~Display_a_map()
{
}
MapQuickView* Display_a_map::mapView()const{
return m_mapView;
}
voidDisplay_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)voidDisplay_a_map::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
m_mapView->setMap(m_map);
setupMap();
emit mapViewChanged();
}
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.
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>usingnamespace Esri::ArcGISRuntime;
Display_a_map::Display_a_map(QObject* parent /* = nullptr */):
QObject(parent),
m_map(newMap(BasemapStyle::ArcGISTopographic, this))
{
}
Display_a_map::~Display_a_map()
{
}
MapQuickView* Display_a_map::mapView()const{
return m_mapView;
}
voidDisplay_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)voidDisplay_a_map::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
m_mapView->setMap(m_map);
setupMap();
emit mapViewChanged();
}
Add a call to a function that reads the map from the mobile map package. You will implement this function in the next step.
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>usingnamespace 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)voidDisplay_an_mmpk::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
setupMapFromMmpk();
emit mapViewChanged();
}
voidDisplay_an_mmpk::setupMapFromMmpk(){
// Instantiate a MobileMapPackage object and establish the path to the MMPK file. MobileMapPackage* m_mobileMapPackage = newMobileMapPackage(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 correctlyif (!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();
}
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.
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>usingnamespace 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)voidDisplay_an_mmpk::setMapView(MapQuickView* mapView){
if (!mapView || mapView == m_mapView)
{
return;
}
m_mapView = mapView;
setupMapFromMmpk();
emit mapViewChanged();
}
voidDisplay_an_mmpk::setupMapFromMmpk(){
// Instantiate a MobileMapPackage object and establish the path to the MMPK file. MobileMapPackage* m_mobileMapPackage = newMobileMapPackage(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 correctlyif (!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.
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.