Take a web map offline.
Use case
Taking a web map offline allows users continued productivity when their network connectivity is poor or nonexistent. For example, by taking a map offline, a field worker inspecting utility lines in remote areas could still access a feature's location and attribute information.
How to use the sample
When the map loads, zoom to the extent you want to take offline. The red border shows the extent that will be downloaded. Tap the "Generate offline map" button to start the offline map job. The progress view will show the job's progress. When complete, the offline map will replace the online map in the map view.
How it works
- Create a
Map
with aPortal
item pointing to the web map. - Create
GenerateOfflineMapParameters
specifying the download area geometry, minimum scale, and maximum scale. - Create an
OfflineMapTask
with the map. - Create the
OfflineMapJob
withOfflineMapTask::generateOfflineMap(params, downloadDirectoryPath)
and start it withOfflineMapJob::start()
. - When the job is done, get the offline map with
OfflineMapJob::result()::offlineMap()
.
Relevant API
- GenerateOfflineMapJob
- GenerateOfflineMapParameters
- GenerateOfflineMapResult
- OfflineMapTask
- Portal
About the data
The map used in this sample shows the stormwater network within Naperville, IL, USA, with cartography designed for web and mobile devices with offline support.
Additional information
The creation of the offline map can be fine-tuned using parameter overrides for feature layers, or by using local basemaps. For examples on these, please consult the samples, "Generate Offline Map (Overrides)" and "Generate offline map with local basemap".
Tags
download, offline, save, web map
Sample Code
// [WriteFile Name=GenerateOfflineMap, Category=Maps]
// [Legal]
// Copyright 2017 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
// http://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.
// [Legal]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
// sample headers
#include "GenerateOfflineMap.h"
// ArcGIS Maps SDK headers
#include "Envelope.h"
#include "Error.h"
#include "GenerateOfflineMapJob.h"
#include "GenerateOfflineMapParameters.h"
#include "GenerateOfflineMapResult.h"
#include "GeometryEngine.h"
#include "Layer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "OfflineMapTask.h"
#include "Point.h"
#include "Portal.h"
#include "PortalItem.h"
#include "SpatialReference.h"
#include "TaskTypes.h"
// Qt headers
#include <QFuture>
#include <QUuid>
using namespace Esri::ArcGISRuntime;
const QString GenerateOfflineMap::s_webMapId = QStringLiteral("acc027394bc84c2fb04d1ed317aac674");
GenerateOfflineMap::GenerateOfflineMap(QQuickItem* parent /* = nullptr */):
QQuickItem(parent)
{
}
void GenerateOfflineMap::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<GenerateOfflineMap>("Esri.Samples", 1, 0, "GenerateOfflineMapSample");
}
void GenerateOfflineMap::componentComplete()
{
QQuickItem::componentComplete();
// find QML MapView component
m_mapView = findChild<MapQuickView*>("mapView");
// Create a Portal Item for use by the Map and OfflineMapTask
bool loginRequired = false;
Portal* portal = new Portal(loginRequired, this);
PortalItem* portalItem = new PortalItem(portal, webMapId(), this);
// Create a map from the Portal Item
m_map = new Map(portalItem, this);
// Update property once map is done loading
connect(m_map, &Map::doneLoading, this, [this](const Error& e)
{
if (!e.isEmpty())
return;
m_mapLoaded = true;
emit mapLoadedChanged();
});
// Set map to map view
m_mapView->setMap(m_map);
// Create the OfflineMapTask with the online map
m_offlineMapTask = new OfflineMapTask(m_map, this);
// connect to the error signal
connect(m_offlineMapTask, &OfflineMapTask::errorOccurred, this, [](const Error& e)
{
if (e.isEmpty())
return;
qDebug() << e.message() << e.additionalMessage();
});
}
void GenerateOfflineMap::generateMapByExtent(double xCorner1, double yCorner1, double xCorner2, double yCorner2)
{
// create an envelope from the QML rectangle corners
const Point corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
const Point corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
const Envelope extent = Envelope(corner1, corner2);
const Envelope mapExtent = geometry_cast<Envelope>(GeometryEngine::project(extent, SpatialReference::webMercator()));
// generate parameters
m_offlineMapTask->createDefaultGenerateOfflineMapParametersAsync(mapExtent).then(this,
[this](const GenerateOfflineMapParameters& params)
{
// Take the map offline once the parameters are generated
GenerateOfflineMapJob* generateJob = m_offlineMapTask->generateOfflineMap(params, m_tempPath.path() + "/offlinemap");
// check if there is a valid job
if (generateJob)
{
// connect to the job's status changed signal
connect(generateJob, &GenerateOfflineMapJob::statusChanged, this, [this, generateJob](JobStatus jobStatus)
{
// connect to the job's status changed signal to know once it is done
switch (jobStatus) {
case JobStatus::Failed:
emit updateStatus("Generate failed");
emit hideWindow(5000, false);
break;
case JobStatus::NotStarted:
emit updateStatus("Job not started");
break;
case JobStatus::Paused:
emit updateStatus("Job paused");
break;
case JobStatus::Started:
emit updateStatus("In progress");
break;
case JobStatus::Succeeded:
// show any layer errors
if (generateJob->result()->hasErrors())
{
QString layerErrors = "";
const QMap<Layer*, Error>& layerErrorsMap = generateJob->result()->layerErrors();
for (auto it = layerErrorsMap.cbegin(); it != layerErrorsMap.cend(); ++it)
{
layerErrors += it.key()->name() + ": " + it.value().message() + "\n";
}
emit showLayerErrors(layerErrors);
}
// show the map
emit updateStatus("Complete");
emit hideWindow(1500, true);
m_mapView->setMap(generateJob->result()->offlineMap(this));
break;
default:
break;
}
});
// connect to progress changed signal
connect(generateJob, &GenerateOfflineMapJob::progressChanged, this, [this, generateJob]()
{
emit updateProgress(generateJob->progress());
});
// connect to the error signal
connect(generateJob, &GenerateOfflineMapJob::errorOccurred, this, [](const Error& e)
{
if (e.isEmpty())
return;
qDebug() << e.message() << e.additionalMessage();
});
// start the generate job
generateJob->start();
}
else
{
emit updateStatus("Export failed");
emit hideWindow(5000, false);
}
});
}