View in QML C++ View on GitHub Sample viewer app
Create and save a map as an ArcGIS PortalItem
(i.e. web map).
Use case
Maps can be created programmatically in code and then serialized and saved as an ArcGIS web map
. A web map
can be shared with others and opened in various applications and APIs throughout the platform, such as ArcGIS Pro, ArcGIS Online, the JavaScript API, Collector, and Explorer.
How to use the sample
Select the basemap and layers you'd like to add to your map.
Press the Save button.
Sign into an ArcGIS Online account.
Provide a title, tags, and description.
Save the map.
How it works
A Map
is created with a Basemap
and a few operational layers.
A Portal
object is created and loaded. This will issue an authentication challenge, prompting the user to provide credentials.
Once the user is authenticated, Map.saveAs
is called and a new Map
is saved with the specified title, tags, and folder.
Relevant API
ArcGIS Online, ArcGIS Pro, portal, publish, share, web map
Sample CodeCreateAndSaveMap.qml CreateAndSaveMap.qml LayerWindow.qml SaveOptionsWindow.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
// [WriteFile Name=CreateAndSaveMap, Category=Maps]
// [Legal]
// Copyright 2018 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 Esri.ArcGISRuntime
import Esri.ArcGISRuntime.Toolkit
Rectangle {
id: rootRectangle
clip : true
width : 800
height : 600
StackView {
id: stackView
anchors.fill : parent
initialItem : layerWindow
}
// MapView that will display the Map created from the user options
MapView {
id: mapView
visible : false
Component.onCompleted : {
// Set the focus on MapView to initially enable keyboard navigation
forceActiveFocus();
}
Button {
anchors {
horizontalCenter : parent .horizontalCenter
bottom : mapView.attributionTop
margins : 5
}
text : "Save map"
onClicked : {
if (portal.loadStatus !== Enums.LoadStatusLoaded)
portal.load();
else {
stackView.push(options);
}
}
}
}
Component {
id: layerWindow
LayerWindow {
onCreateMapSelected : (basemap, layerList) => {
stackView.push(mapView)
const map = createMap(basemap, layerList);
mapView.map = map;
}
}
}
// Window to display options for setting title, tags, and description
Component {
id: options
SaveOptionsWindow {
onCancelClicked : {
stackView.pop();
}
onSaveMapClicked : (title, tags, description) => {
const thumbnail = "" ;
const folder = null ;
const forceSave = true ;
const tagsList = tags.split( "," );
mapView.map.saveAs(portal, title, tagsList, forceSave, folder, description, thumbnail);
}
}
}
// Rectangle to display completion text
Component {
id: completionRect
Rectangle {
property alias text : completeText.text
Text {
id: completeText
anchors.centerIn : parent
textFormat : Text.RichText
horizontalAlignment : Text.AlignHCenter
onLinkActivated : link => Qt.openUrlExternally(link)
}
Button {
anchors {
horizontalCenter : parent .horizontalCenter
bottom : parent .bottom
margins : 5
}
text : "Create New Map"
onClicked : {
// We need a local ref to the stackView and layerWindow
// object as our object references will have been deleted
// once "clear" cleans up this object.
const sv = stackView;
const lWindow = layerWindow;
sv.clear();
sv.push(lWindow);
}
}
}
}
// Create Portal object that requires sign in
Portal {
id: portal
url : "https://www.arcgis.com"
loginRequired : true
onLoadStatusChanged : {
if (loadStatus === Enums.LoadStatusLoaded) {
stackView.push(options);
}
}
}
// Declare AuthenticationView to handle any authentication challenges
AuthenticationView {
anchors.fill : parent
}
function createMap ( basemap, layerList ) {
// Create the Basemap
let selectedBasemap;
if (basemap === "Streets" )
selectedBasemap = Enums.BasemapStyleArcGISStreets;
else if (basemap === "Imagery" )
selectedBasemap = Enums.BasemapStyleArcGISImagery;
else if (basemap === "Topographic" )
selectedBasemap = Enums.BasemapStyleArcGISTopographic;
else if (basemap === "Oceans" )
selectedBasemap = Enums.BasemapStyleArcGISOceans;
// Create the Map with the basemap
selectedBasemap = ArcGISRuntimeEnvironment.createObject( "Basemap" , { initStyle : selectedBasemap});
const map = ArcGISRuntimeEnvironment.createObject( "Map" , { basemap : selectedBasemap }, mapView);
map.saveStatusChanged.connect(()=> {
if (map.saveStatus === Enums.TaskStatusCompleted) {
const url = "https://www.arcgis.com/home/item.html?id=%1" .arg(map.item.itemId);
stackView.push(completionRect,
{ text : 'Map saved successfully.<br>View in <a href="%1">ArcGIS Online</a>' .arg( url ) });
} else if (map.saveStatus === Enums.TaskStatusErrored) {
if (stackView.currentItem === completionRect)
return ;
stackView.push(completionRect,
{ text : 'An error occurred while saving the map. Details: %1 %2' .arg(map.error.message).arg(map.error.additionalMessage) });
}
});
// Add Operational Layers
for ( let i = 0 ; i < layerList.length; i++) {
if (layerList[i] === "WorldElevations" ) {
const elevationLyr = ArcGISRuntimeEnvironment.createObject( "ArcGISMapImageLayer" , {
url : "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Elevation/WorldElevations/MapServer"
});
elevationLyr.opacity = 0.5 ;
map.operationalLayers.append(elevationLyr);
} else if (layerList[i] === "Census" ) {
map.operationalLayers.append(ArcGISRuntimeEnvironment.createObject( "ArcGISMapImageLayer" , {
url : "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Census/MapServer"
}));
}
}
// Return the Map
return map;
}
}