Create and save a map

View on GitHubSample viewer app

Create and save a map as a portal item (i.e. web map).

Image of create and save map 1 Image of create and save map 2 Image of create and save map 3 Image of create and save map 4

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

Enter a username and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). Select the basemap and layers to add to the map. Tap the "Save" button. Provide a title, tags, description, and folder. Save the map.

How it works

  1. Add an AGSOAuthConfiguration to the shared AGSAuthenticationManager's oAuthConfigurations array and remove all credentials from the credentialCache.
  2. Create a new AGSPortal instance and load it to invoke the authentication challenge.
  3. Access the user's portal content by using AGSPortalUser.fetchContent(completion:). Then get the array of AGSPortalFolders.
  4. Create an AGSMap with an AGSBasemapStyle and a few operational layers.
  5. Save the map by using AGSMap.save(as:portal:tags:folder:itemDescription:thumbnail:forceSaveToSupportedVersion:completion:) and a new map is saved with the specified title, tags, and folder.

Relevant API

  • AGSMap
  • AGSMap.save
  • AGSPortal

Tags

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

Sample Code

CreateOptionsViewController.swiftCreateOptionsViewController.swiftCreateSaveMapViewController.swiftSaveAsViewController.swift
Use dark colors for code blocksCopy
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
// Copyright 2016 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 UIKit
import ArcGIS

protocol CreateOptionsViewControllerDelegate: AnyObject {
    func createOptionsViewController(_ createOptionsViewController: CreateOptionsViewController, didSelectBasemap basemap: AGSBasemap, layers: [AGSLayer])
}

class CreateOptionsViewController: UITableViewController {
    private let basemapStyles: KeyValuePairs<String, AGSBasemapStyle> = [
        "Streets": .arcGISStreets,
        "Imagery": .arcGISImageryStandard,
        "Topographic": .arcGISTopographic,
        "Oceans": .arcGISOceans
    ]
    private let layers: [AGSLayer] = {
        let layerURLs = [
            URL(string: "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Elevation/WorldElevations/MapServer")!,
            URL(string: "https://sampleserver5.arcgisonline.com/arcgis/rest/services/Census/MapServer")!
        ]
        return layerURLs.map { AGSArcGISMapImageLayer(url: $0) }
    }()

    private var selectedBasemapIndex: Int = 0
    private var selectedLayerIndices: IndexSet = []

    weak var delegate: CreateOptionsViewControllerDelegate?

    // MARK: - UITableViewDataSource

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return section == 0 ? "Choose Basemap" : "Add Operational Layers"
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return section == 0 ? basemapStyles.count : layers.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "OptionCell", for: indexPath)
        switch indexPath.section {
        case 0:
            let basemapStyle = basemapStyles[indexPath.row]
            cell.textLabel?.text = basemapStyle.key

            // Accesory view.
            if selectedBasemapIndex == indexPath.row {
                cell.accessoryType = .checkmark
            } else {
                cell.accessoryType = .none
            }
        default:
            let layer = layers[indexPath.row]
            cell.textLabel?.text = layer.name

            // Accessory view.
            if selectedLayerIndices.contains(indexPath.row) {
                cell.accessoryType = .checkmark
            } else {
                cell.accessoryType = .none
            }
        }
        return cell
    }

    // MARK: - UITableViewDelegate

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            // Create a IndexPath for the previously selected index.
            let previousSelectionIndexPath = IndexPath(row: selectedBasemapIndex, section: 0)
            selectedBasemapIndex = indexPath.row
            tableView.reloadRows(at: [indexPath, previousSelectionIndexPath], with: .none)
        case 1:
            // Check if already selected.
            if selectedLayerIndices.contains(indexPath.row) {
                // Remove the selection.
                selectedLayerIndices.remove(indexPath.row)
            } else {
                selectedLayerIndices.update(with: indexPath.row)
            }
            tableView.reloadRows(at: [indexPath], with: .none)
        default:
            break
        }
    }

    // MARK: - Actions

    @IBAction func cancelAction() {
        // Dissmiss the view if canceled is tapped.
        dismiss(animated: true)
    }

    @IBAction private func doneAction() {
        // Create a basemap with the selected basemap index.
        let basemap = AGSBasemap(style: basemapStyles[selectedBasemapIndex].value)

        // Create an array of the selected operational layers.
        let selectedLayers = selectedLayerIndices.map { layers[$0].copy() as! AGSLayer }

        delegate?.createOptionsViewController(self, didSelectBasemap: basemap, layers: selectedLayers)
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.