You will learn: how to build an app that loads a basemap and feature layers from a mobile map package file.
Offline maps allow users to load .mmpk
files) can be created in MobileMapPackage
class.
A mobile map package is a transport mechanism for maps, their layers, and the layer's data. It contains metadata about the package (description, thumbnail, and others), one or more maps, layers, data, and optionally networks and locators.
In this tutorial you will learn how to load a mobile map package and then use it in your app.
You must have previously installed the ArcGIS Runtime SDK for Qt and set up the development environment for your operating system.
If you have already completed the Create a starter app tutorial, start Qt Creator and open your starter app project. Otherwise, download and unzip the starter app project solution, and then open it in Qt Creator.
You will need a mobile map package (.mmpk
) file to complete this tutorial. Learn how to create mobile map packages in the Prepare your data for offline use tutorial. Alternatively, download the map package we prepared, and save the file on your development machine.
/your_local_path/offline-maps-package.mmpk
is a placeholder in the code below. Change this to match your file system.In Projects, double click on Headers > Create_a_starter_app.h, forward declare MobileMapPackage
by adding class MobileMapPackage;
to the namespace along with an additional include statement.
class MapQuickView;
// *** ADD ***
class MobileMapPackage;
}
}
#include <QObject>
// *** ADD ***
#include <QString>
Add the public member function openMapPackage
. This function will asynchronously open the map package and set it on the map view.
public:
explicit Create_a_starter_app(QObject* parent = nullptr);
~Create_a_starter_app() override;
// *** ADD ***
void openMapPackage(const QString& dataPath);
Add the private member variable m_mmpk
to hold a reference to the map package.
Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr;
// *** ADD ***
Esri::ArcGISRuntime::MobileMapPackage* m_mmpk = nullptr;
In Projects, double click on Sources > Create_a_starter_app.cpp to open it in the editor. Comment out #include "Basemap.h"
because the map package will include the basemap definition. Add an include for MobileMapPackage
so that you can reference the class in your code.
// *** UPDATE ***
// #include "Basemap.h"
#include "Map.h"
// *** ADD ***
#include "MobileMapPackage.h"
You are displaying an offline map package, and Basemap is an online service. Accordingly, update the constructor to remove m_map
from the initializer list in the constructor. Also remove the center
and viewpoint
objects and replace the body of the constructor with following code to open the map package.
Create_a_starter_app::Create_a_starter_app(QObject* parent /* = nullptr */):
// *** UPDATE ***
// QObject(parent),
// m_map(new Map(Basemap::topographicVector(this), this))
QObject(parent)
{
// *** UPDATE ***
// const Point center(-118.71511, 34.09042, SpatialReference::wgs84());
// const Viewpoint viewpoint(center, 300000.0);
// set the path below to your local path to this file
const QString mmpk_path("/your_local_path/offline-maps-package.mmpk");
openMapPackage(mmpk_path);
}
Add the public member function openMapPackage
to open a MobileMapPackage
and wait until it loads to add it to the map view.
void Create_a_starter_app::openMapPackage(const QString& dataPath)
{
m_mmpk = new MobileMapPackage(dataPath, this);
connect(m_mmpk, &MobileMapPackage::doneLoading, this, [this](Error e) {
if (!e.isEmpty())
{
qDebug() << QString("Error loading package: %1 %2").arg(e.message(), e.additionalMessage());
return;
}
if (!m_mmpk || !m_mapView || m_mmpk->maps().isEmpty())
{
return;
}
m_mapView->setMap(m_mmpk->maps().at(0));
});
m_mmpk->load();
}
Run your code to load the offline map.
Your map, as it was designed in ArcGIS Pro, should load and display with the view point centered on the Santa Monica Mountains. Compare your map with our completed solution project.
Learn how you can change many elements of your map without writing any code. Return to ArcGIS Pro and alter your map: change the extent, add or remove layers, and change the symbols. Republish your map package and load it on your device.
Add a map file picker to allow your user to select the map package. Try starting with QML's FileDialog
.