Overview

You will learn: how to load a layer from an ArcGIS Online item and add it to a map.

The ArcGIS Runtime SDK for Java allows you to access items (such as layers) stored in ArcGIS Online by their item identifier. Feature layers can store geographic features (points, lines, polygons), edit or update attributes, and synchronize changes. As demonstrated in the lab Display a web map, you can load and display an existing web map that was preconfigured with a basemap and layers. Alternatively, you may wish to load one or more layers from ArcGIS Online and create a map with code. You may do this if you have operational or base layers from different sources in your app. You can publish your own feature layer to ArcGIS Online using the Create a new dataset data lab.

In this lab you will learn how to access a layer stored in ArcGIS Online. You will open the layer using its identifier and display its contents on a map.

Before you begin

Reuse the starter project

If you have not done the starter project lab, be sure to review it first so you are familiar with running the app.

In a new or empty folder, make a copy of the Create a starter app solution.

Steps

Update the project

  1. Open the settings.gradle file in an editor of your choice and change the rootProject.name value to add-a-layer-from-an-item.

Imports

  1. Open the App.java file in src/main/java/com/arcgis/developers/labs. Then add the following with the existing imports.
    import javafx.scene.control.Alert;
    import javafx.scene.control.Alert.AlertType;
    
    import com.esri.arcgisruntime.layers.FeatureLayer;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.portal.Portal;
    import com.esri.arcgisruntime.portal.PortalItem;
    

Create and load portal item from portal

  1. Add a private method setupPortalItem that creates a Portal that establishes a connection to ArcGISOnline.

     private void setupPortalItem() {
       String portalItemId = "883cedb8c9fe4524b64d47666ed234a7";
       Portal portal = new Portal("https://www.arcgis.com");
     }
    
  2. Create a PortalItem using the portal and a unique id number. This is the unique id for the Los Angeles Trailheads portal item.

     Portal portal = new Portal("https://www.arcgis.com");
     /* ** ADD ** */
     PortalItem portalItem = new PortalItem(portal, portalItemId);
    
  3. Wait for portal item to load and check load status.

     PortalItem portalItem = new PortalItem(portal, "883cedb8c9fe4524b64d47666ed234a7");
     /* ** ADD ** */
     portalItem.addDoneLoadingListener(() -> {
       if (portalItem.getLoadStatus() == LoadStatus.LOADED) {
         //setupFeatureLayerFromPortalItem(portalItem);
       } else {
         new Alert(AlertType.ERROR, "Portal Item: " + portalItem.getLoadError().getMessage()).show();
       }
     });
     portalItem.loadAsync();
    

Create layer from portal item and add to map

  1. Add a private method setupFeatureLayerFromPortalItem that creates a feature layer from a portal item and layer id.

     private void setupFeatureLayerFromPortalItem(PortalItem portalItem) {
       int layerId = 0;
       FeatureLayer layer = new FeatureLayer(portalItem, layerId);
     }
    
  2. Wait for layer to load then add layer to the map and set viewpoint to display all features within layer.

     FeatureLayer layer = new FeatureLayer(portalItem, 0);
     /* ** ADD ** */
     layer.addDoneLoadingListener(() -> {
       if (layer.getLoadStatus() == LoadStatus.LOADED) {
         mapView.getMap().getOperationalLayers().add(layer);
         mapView.setViewpoint(new Viewpoint(layer.getFullExtent()));
       } else {
         new Alert(AlertType.ERROR, "Feature Layer: " + layer.getLoadError().getMessage()).show();
       }
     });
     layer.loadAsync();
    
  3. Uncomment the call to setupFeatureLayerFromPortalItem in setupPortalItem. In the start method, add a call to setupPortalItem after setupMap as follows:

    setupMap();
     /* ** ADD ** */
    setupPortalItem();
    

Congratulations, you're done!

Your map and layer should load and display with the view point centered on the Los Angeles area and points displaying the trailheads.

Challenge

Use your portal item

Instead of using the portal item we used above, use the one you made in the data lab Create a new dataset.

In setupPortalItem method, replace the value of portalItemId with the id of your portal item.

Find the portal item URL

There are multiple ways to load a feature layer from ArcGIS Online. One way is to load the feature layer directly from a URL. This challenge is a follow-on to the challenge in Create a new dataset design lab.

Navigate to the page in ArcGIS Online for your portal item. In the item page, find the Service URL link and identify the service endpoint. This is the URL the runtime uses to access the data. It should look something like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0

Store the URL:

 // Hold the URL to the service
 String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0";

Create the ServiceFeatureTable from URL with FeatureLayer

// Create the layer
ServiceFeatureTable table = new ServiceFeatureTable(url);
FeatureLayer layer = new FeatureLayer(table);

Add the layer to the map

mapView.getMap().getOperationalLayers.add(layer);

Discover data

If you have not already done so, visit the Discover data lab and search for interesting open data you could add in another layer in your app.