View in QML C++ View on GitHub Sample viewer app
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 a PortalItem
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
with OfflineMapTask.generateOfflineMap(params, downloadDirectoryPath)
and start it with OfflineMapJob.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.
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".
download, offline, save, web map
Sample CodeGenerateOfflineMap.qml GenerateOfflineMap.qml DownloadButton.qml GenerateWindow.qml
Use dark colors for code blocks Copy
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// [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]
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Esri.ArcGISRuntime
import Esri.ArcGISExtras
Rectangle {
id: rootRectangle
clip : true
width : 800
height : 600
readonly property url outputMapPackagePath : System.temporaryFolder.url + "/OfflineMap_%1" .arg( new Date ().getTime().toString())
readonly property string webMapId : "acc027394bc84c2fb04d1ed317aac674"
MapView {
id: mapView
anchors.fill : parent
Component.onCompleted : {
// Set the focus on MapView to initially enable keyboard navigation
forceActiveFocus();
}
// Create a Map from a Portal Item
Map {
id: map
PortalItem {
id: mapPortalItem
itemId : webMapId
Portal {
loginRequired : false
}
}
}
// Create a button and anchor it to the attribution top
DownloadButton {
id: downloadButton
anchors {
horizontalCenter : parent .horizontalCenter
bottom : mapView.attributionTop
margins : 5
}
visible : map.loadStatus === Enums.LoadStatusLoaded
onButtonClicked : extentRectangle.getRectangleEnvelope();
}
}
// Create an extent rectangle for selecting the offline area
Rectangle {
id: extentRectangle
anchors.centerIn : parent
width : parent .width - 50
height : parent .height - 125
color : "transparent"
visible : map.loadStatus === Enums.LoadStatusLoaded
border {
color : "red"
width : 3
}
function getRectangleEnvelope ( ) {
const corner1 = mapView.screenToLocation(extentRectangle.x, extentRectangle.y);
const corner2 = mapView.screenToLocation((extentRectangle.x + extentRectangle.width), (extentRectangle.y + extentRectangle.height));
const envBuilder = ArcGISRuntimeEnvironment.createObject( "EnvelopeBuilder" );
envBuilder.setCorners(corner1, corner2);
const mapExtent = GeometryEngine.project(envBuilder.geometry, Factory.SpatialReference.createWebMercator());
offlineMapTask.createDefaultGenerateOfflineMapParameters(mapExtent);
}
}
// Create Offline Map Task
OfflineMapTask {
id: offlineMapTask
onlineMap : map
property var generateJob
onErrorChanged : console .log( "error:" , error.message, error.additionalMessage);
onCreateDefaultGenerateOfflineMapParametersStatusChanged : {
if (createDefaultGenerateOfflineMapParametersStatus !== Enums.TaskStatusCompleted)
return ;
// Take the map offline once the parameters are generated
takeMapOffline(offlineMapTask.createDefaultGenerateOfflineMapParametersResult);
}
function takeMapOffline ( parameters ) {
// create the job
generateJob = offlineMapTask.generateOfflineMap(parameters, outputMapPackagePath);
// check if job is valid
if (generateJob) {
// show the export window
generateWindow.visible = true ;
// connect to the job's status changed signal to know once it is done
generateJob.statusChanged.connect(updateJobStatus);
// connect to the job's progress changed signal
generateJob.progressChanged.connect(updateProgress);
generateJob.start();
} else {
generateWindow.visible = true ;
generateWindow.statusText = "Task failed" ;
generateWindow.hideWindow( 5000 );
}
}
function updateJobStatus ( ) {
switch (generateJob.jobStatus) {
case Enums.JobStatusFailed :
generateWindow.statusText = "Task failed" ;
generateWindow.hideWindow( 5000 );
break ;
case Enums.JobStatusNotStarted :
generateWindow.statusText = "Job not started" ;
break ;
case Enums.JobStatusPaused :
generateWindow.statusText = "Job paused" ;
break ;
case Enums.JobStatusStarted :
generateWindow.statusText = "In progress" ;
break ;
case Enums.JobStatusSucceeded :
// show any layer errors
if (generateJob.result.hasErrors) {
const layerErrors = generateJob.result.layerErrors;
let errorText = "" ;
for ( let i = 0 ; i < layerErrors.length; i++) {
const errorPair = layerErrors[i];
errorText += errorPair.layer.name + ": " + errorPair.error.message + "\n" ;
}
msgDialog.detailedText = errorText;
msgDialog.open();
}
// show the map
generateWindow.statusText = "Complete" ;
generateWindow.hideWindow( 1500 );
displayOfflineMap(generateJob.result);
break ;
default :
console .log( "default" );
break ;
}
}
function updateProgress ( ) {
generateWindow.progressText = generateJob.progress;
}
function displayOfflineMap ( result ) {
// Set the offline map to the MapView
mapView.map = result.offlineMap;
downloadButton.visible = false ;
extentRectangle.visible = false ;
}
Component.onDestruction : {
if (generateJob) {
generateJob.statusChanged.disconnect(updateJobStatus);
generateJob.progressChanged.disconnect(updateProgress);
}
}
}
GenerateWindow {
id: generateWindow
anchors.fill : parent
}
Dialog {
id: msgDialog
modal : true
x : Math .round( parent .width - width) / 2
y : Math .round( parent .height - height) / 2
standardButtons : Dialog.Ok
title : "Layer Errors"
property alias text : textLabel.text
property alias detailedText : detailsLabel.text
ColumnLayout {
Text {
id: textLabel
text : "Some layers could not be taken offline."
}
Text {
id: detailsLabel
}
}
}
}