Create and save map

View on GitHubSample viewer app

Create and save a map as a web map item to an ArcGIS portal.

Image of create and save map

Use case

Maps can be created programmatically in code and then serialized and saved as an ArcGIS portal item. In this case, the portal item is a web map which 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

When you run the sample, you will be challenged for an ArcGIS Online login. Enter a username and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). Then, choose the basemap and layers for your new map. To save the map, choose a title, tags and description (optional), and a folder on your portal (you will need to create one in your portal's My Content section if you don't already have one). Click the Save button to save the map to the chosen folder.

How it works

  1. Set a DefaultAuthenticationChallengeHandler to the app's AuthenticationManager.
  2. Create a new Portal and load it to invoke the authentication challenge.
  3. Access the PortalUserContent with portal.getUser().fetchContentAsync().get(), to get the user's list of portal folders with portalUserContent.getFolders().
  4. Create an ArcGISMap with a BasemapStyle and a few operational layers.
  5. Call map.saveMapAsAsync() to save a new ArcGISMap with the specified title, tags, and folder to the portal.

Relevant API

  • ArcGISMap
  • Portal

Tags

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

Sample Code

CreateAndSaveMapController.javaCreateAndSaveMapController.javaCreateAndSaveMapSample.java
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
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
215
216
/*
 * Copyright 2019 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.
 */

package com.esri.samples.create_and_save_map;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.util.StringConverter;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalFolder;
import com.esri.arcgisruntime.portal.PortalItem;
import com.esri.arcgisruntime.portal.PortalUserContent;
import com.esri.arcgisruntime.security.AuthenticationManager;
import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;

public class CreateAndSaveMapController {

  @FXML private MapView mapView;
  @FXML private TextField title;
  @FXML private TextField tags;
  @FXML private TextArea description;
  @FXML private ComboBox<PortalFolder> folderList;
  @FXML private ListView<BasemapStyle> basemapStyleListView;
  @FXML private ListView<Layer> layersList;
  @FXML private Button saveButton;
  @FXML private ProgressIndicator progress;
  @FXML private VBox vBox;

  private ArcGISMap map;
  private Portal portal;

  @FXML
  private void initialize() {

    // set up the authentication manager to handle authentication challenges
    DefaultAuthenticationChallengeHandler defaultAuthenticationChallengeHandler = new DefaultAuthenticationChallengeHandler();
    AuthenticationManager.setAuthenticationChallengeHandler(defaultAuthenticationChallengeHandler);

    portal = new Portal("https://www.arcgis.com", true);
    portal.addDoneLoadingListener(() -> {
      if (portal.getLoadStatus() == LoadStatus.LOADED) {
        try {
          // get the users list of portal folders
          PortalUserContent portalUserContent = portal.getUser().fetchContentAsync().get();
          List<PortalFolder> portalFolders = portalUserContent.getFolders();
          folderList.getItems().addAll(portalFolders);
        } catch (Exception e) {
          e.printStackTrace();
        }

        saveButton.setDisable(false);

        // set the basemap style options
        basemapStyleListView.getItems().addAll(
          BasemapStyle.ARCGIS_STREETS,
          BasemapStyle.ARCGIS_IMAGERY_STANDARD,
          BasemapStyle.ARCGIS_TOPOGRAPHIC,
          BasemapStyle.ARCGIS_OCEANS);

        basemapStyleListView.setCellFactory(c -> new BasemapCell());
        // update the basemap when the selection changes
        basemapStyleListView.getSelectionModel().selectFirst();
        basemapStyleListView.getSelectionModel().selectedItemProperty().addListener(o -> {

          Basemap selectedBasemap = new Basemap(basemapStyleListView.getSelectionModel().getSelectedItem());
          map.setBasemap(selectedBasemap);
        });

        // create a map with the first basemap style option, and authenticate the basemap to access it using your API key
        map = new ArcGISMap(new Basemap(basemapStyleListView.getSelectionModel().getSelectedItem()));
        mapView.setMap(map);
        mapView.setViewInsets(new Insets(0, 0, 0, vBox.getWidth()));

        // set operational layer options
        ArcGISMapImageLayer worldElevation = new ArcGISMapImageLayer(
          "https://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer");
        worldElevation.loadAsync();

        ArcGISMapImageLayer worldCensus = new ArcGISMapImageLayer(
          "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer");
        worldCensus.loadAsync();

        layersList.getItems().addAll(worldElevation, worldCensus);
        layersList.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        layersList.getSelectionModel().selectedItemProperty().addListener(o -> {
          map.getOperationalLayers().clear();
          map.getOperationalLayers().addAll(layersList.getSelectionModel().getSelectedItems());
        });

        layersList.setCellFactory(c -> new LayerCell());

        // set portal folder title converter
        folderList.setConverter(new StringConverter<>() {

          @Override
          public String toString(PortalFolder folder) {
            return folder != null ? folder.getTitle() : "";
          }

          @Override
          public PortalFolder fromString(String string) {
            return null;
          }
        });

      } else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {

        // show alert message on error
        new Alert(Alert.AlertType.ERROR, "Authentication failed: " + portal.getLoadError().getMessage()).show();
      }
    });

    // load the portal info of a secured resource. This will invoke the authentication challenge
    portal.loadAsync();

  }

  /**
   * Shows a BasemapStyle title in a ListView.
   */
  private static class BasemapCell extends ListCell<BasemapStyle> {

    @Override
    protected void updateItem(BasemapStyle basemapStyle, boolean empty) {
      super.updateItem(basemapStyle, empty);
      setText(empty || basemapStyle == null ? null : basemapStyle.name());
      setGraphic(null);
    }
  }

  /**
   * Shows a Layer title in a ListView.
   */
  private static class LayerCell extends ListCell<Layer> {

    @Override
    protected void updateItem(Layer layer, boolean empty) {
      super.updateItem(layer, empty);
      setText(empty || layer == null ? null : layer.getName());
      setGraphic(null);
    }
  }

  /**
   * Save the map to the portal and raise an exception if save not successful.
   */
  @FXML
  private void saveMap() {
    progress.setVisible(true);
    try {
      ListenableFuture<PortalItem> result = map.saveAsAsync(portal, folderList.getSelectionModel().getSelectedItem(),
        title.getText(), Arrays.asList(tags.getText().split(",")), description.getText(), null, true);
      result.addDoneListener(() -> {
        try {
          PortalItem portalItem = result.get();
          new Alert(Alert.AlertType.CONFIRMATION, "Map titled " + title.getText() + " saved to portal item with id: " + portalItem.getItemId()).show();
        } catch (InterruptedException | ExecutionException e) {
          new Alert(Alert.AlertType.ERROR, "Save Unsuccessful: " + e.getCause().getMessage()).show();
        } finally {
          progress.setVisible(false);
        }
      });
    } catch (Exception e) {
      progress.setVisible(false);
      new Alert(Alert.AlertType.ERROR, "Could not save map. " + e.getMessage()).show();
    }
  }

  /**
   * Stops and releases all resources used in application.
   */
  void terminate() {

    if (mapView != null) {
      mapView.dispose();
    }
  }

}

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