Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for Qt

Create and save map

Sample Viewer View Sample on GitHub

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

  1. Select the basemap and layers you'd like to add to your map.
  2. Press the Save button.
  3. Sign into an ArcGIS Online account.
  4. Provide a title, tags, and description.
  5. Save the map.

How it works

  1. A Map is created with a Basemap and a few operational layers.
  2. A Portal object is created and loaded. This will issue an authentication challenge, prompting the user to provide credentials.
  3. Once the user is authenticated, Map.saveAs is called and a new Map is saved with the specified title, tags, and folder.

Relevant API

  • Map
  • Map.saveAs
  • Portal

Tags

ArcGIS Online, ArcGIS Pro, portal, publish, share, web map

Sample Code

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.9
import Esri.ArcGISRuntime.Toolkit.Dialogs 100.9

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

        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: {
                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: {
                const thumbnail = null;
                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: 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
        authenticationManager: AuthenticationManager
    }
    */

    function createMap(basemap, layerList) {
        // Create the Basemap
        let selectedBasemap;
        if (basemap === "Streets")
            selectedBasemap = ArcGISRuntimeEnvironment.createObject("BasemapStreets");
        else if (basemap === "Imagery")
            selectedBasemap = ArcGISRuntimeEnvironment.createObject("BasemapImagery");
        else if (basemap === "Topographic")
            selectedBasemap = ArcGISRuntimeEnvironment.createObject("BasemapTopographic");
        else if (basemap === "Oceans")
            selectedBasemap = ArcGISRuntimeEnvironment.createObject("BasemapOceans");

        // Create the Map with the basemap
        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;
    }
}
// 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.

import QtQuick 2.6
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2
import QtGraphicalEffects 1.0

Rectangle {
    RadialGradient {
        anchors.fill: parent
        opacity: 0.5
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 0.7; color: "black" }
        }
    }

    signal createMapSelected(var basemap, var layerList)

    Rectangle {
        color: "#edeeef"
        radius: 5
        width: childrenRect.width
        height: childrenRect.height
        anchors.centerIn: parent
        border {
            color: "#77787a"
            width: 1
        }

        ColumnLayout {
            id: layerColumn
            Text {
                Layout.margins: 5
                Layout.alignment: Qt.AlignHCenter
                text: "Select Layers"
                font {
                    pixelSize: 18
                    family: "helvetica"
                }
            }

            Text {
                Layout.margins: 5
                text: "Select Basemap:"
                font {
                    pixelSize: 14
                    family: "helvetica"
                }
            }

            ComboBox {
                id: basemapComboBox
                property int modelWidth: 0
                Layout.minimumWidth: modelWidth + leftPadding + rightPadding + indicator.width
                Layout.margins: 5
                Layout.fillWidth: true
                model: ["Streets", "Imagery", "Topographic", "Oceans"]

                Component.onCompleted : {
                    for (let i = 0; i < model.length; ++i) {
                        metrics.text = model[i];
                        modelWidth = Math.max(modelWidth, metrics.width);
                    }
                }
                TextMetrics {
                    id: metrics
                    font: basemapComboBox.font
                }
            }

            Text {
                Layout.margins: 5
                text: "Select Operational Layers:"
                font {
                    pixelSize: 14
                    family: "helvetica"
                }
            }

            Repeater {
                id: operationalLayerRepeater
                Layout.margins: 5
                Layout.fillWidth: true
                model: ["WorldElevations", "Census"]

                CheckBox {
                    text: modelData
                    checked: true
                }
            }

            Button {
                Layout.margins: 5
                Layout.fillWidth: true
                text: "Create Map"
                onClicked: {
                    const layerList = [];
                    for (let i = 0; i < operationalLayerRepeater.count; i++) {
                        const currentCheckbox = operationalLayerRepeater.itemAt(i);
                        if (currentCheckbox.checked) {
                            layerList.push(currentCheckbox.text)
                        }
                    }
                    createMapSelected(basemapComboBox.currentText, layerList);
                }
            }
        }
    }
}
// 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.

import QtQuick 2.6
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0

Rectangle {
    RadialGradient {
        anchors.fill: parent
        opacity: 0.5
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white" }
            GradientStop { position: 0.7; color: "black" }
        }
    }

    signal saveMapClicked(var title, var tags, var description)
    signal cancelClicked()

    Rectangle {
        color: "#edeeef"
        radius: 5
        anchors.centerIn: parent
        width: childrenRect.width
        height: childrenRect.height
        border {
            color: "#77787a"
            width: 1
        }

        GridLayout {
            id: layerColumn
            columns: 2

            Text {
                id: title
                Layout.columnSpan: 2
                Layout.margins: 5
                text: "Title*:"
                font {
                    pixelSize: 14
                    family: "helvetica"
                }
            }

            TextField {
                id: titleText
                selectByMouse: true
                Layout.columnSpan: 2
                Layout.margins: 5
                Layout.fillWidth: true

                placeholderText: "ex: Sample Map"
            }

            Text {
                id: tags
                Layout.columnSpan: 2
                Layout.margins: 5
                text: "Tags:"
                font {
                    pixelSize: 14
                    family: "helvetica"
                }
            }

            TextField {
                id: tagsText
                selectByMouse: true
                Layout.columnSpan: 2
                Layout.margins: 5
                Layout.fillWidth: true
                placeholderText: "ex: map, sample, elevation"
            }

            Text {
                id: description
                Layout.columnSpan: 2
                Layout.margins: 5
                text: "Description:"
                font {
                    pixelSize: 14
                    family: "helvetica"
                }
            }

            TextField {
                id: descriptionText
                selectByMouse: true
                Layout.columnSpan: 2
                Layout.margins: 5
                Layout.fillWidth: true
                placeholderText: "ex: This map displays..."
            }

            Text {
                Layout.columnSpan: 2
                Layout.margins: 5
                text: "*Field is Required"
                font {
                    pixelSize: 10
                    family: "helvetica"
                }
            }

            Button {
                Layout.margins: 5
                text: "Save"
                onClicked: {
                    // Make sure a Title is supplied
                    if (titleText.text === "") {
                        title.color = "red";
                        return;
                    }

                    saveMapClicked(titleText.text, tagsText.text, descriptionText.text)
                }
            }

            Button {
                Layout.margins: 5
                text: "Cancel"
                onClicked: cancelClicked()
            }
        }
    }
}