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 Android allows you to access items (such as layers) stored in ArcGIS Online by their item identifier. Using feature layers, you can store geographic features (points, lines, polygons), edit or update attributes, and synchronize changes. As demonstrated in the Display a web map lab, 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

Make sure you have installed the latest version of Android Studio.

Reuse the starter project

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

  • Open the new Android project in Android Studio.

  • Run the project and verify the project builds and the map displays on the device or emulator.

  • You may be required to sync the project dependencies.

Steps

Add a new class member

  1. In the file app > java > {your.package.name} > MainActivity.java, define a class-level FeatureLayer variable entitled mLayer. Remember to use Alt + Enter on Windows (Option + Enter on Mac) to import the FeatureLayer class.

    private FeatureLayer mLayer;
    

Create the portal

  1. In the MainActivity.java file, change the Basemap.Type to TOPOGRAPHIC in the setUpMap() method.

    Basemap.Type basemapType = Basemap.Type.TOPOGRAPHIC;
    
  2. Add a new method to the MainActivity.java class for adding a layer to the map. This method accepts an ArcGISMap as a parameter.

        private void addLayer(final ArcGISMap map) { }
    
  3. The Portal class provides a way to build applications that work with content from the cloud with ArcGIS Online or on-premises with ArcGIS Enterprise. Create the Portal object for anonymous access using the portal URL. ArcGIS Online's portal URL is http://www.arcgis.com.

    Portal portal = new Portal("http://www.arcgis.com");
    

Load the portal item

  1. Create a variable to store an ArcGIS Online PortalItem. The PortalItem represents content stored in ArcGIS Online - in this case, the Los Angeles Trailheads portal item (feature layer).

    PortalItem portalItem = new PortalItem(portal,"883cedb8c9fe4524b64d47666ed234a7");
    

Create the layer from the portal item

  1. Create the layer using the portal item and its layer id.

    The FeatureLayer must be loaded explicitly before it can be used in the map by calling loadAsync() on the feature layer and attaching a listener that is invoked when loading has completed.

    mLayer = new FeatureLayer(portalItem, 0);
    mLayer.loadAsync();
    mLayer.addDoneLoadingListener(new Runnable() {
     });
    

Add the layer to the map

  1. Ensure the layer is loaded and use it to update the passed in Map's inital Viewpoint.

    private void addLayer(final ArcGISMap map){
        Portal portal = new Portal("http://www.arcgis.com");
        final PortalItem portalItem = new PortalItem(portal, "883cedb8c9fe4524b64d47666ed234a7");
        mLayer = new FeatureLayer(portalItem,0);
        mLayer.loadAsync();
        mLayer.addDoneLoadingListener(new Runnable() {
        // *** ADD ***
            @Override public void run() {
                if (mLayer.getLoadStatus() == LoadStatus.LOADED) {
                    Viewpoint viewpoint = new Viewpoint(mLayer.getFullExtent());
                    map.setInitialViewpoint(viewpoint);
                }
            }
        });
    
  2. After setting the Viewpoint, add the new FeatureLayer to the Map's operational layers and set the Map to the MapView.

    private void addLayer(final ArcGISMap map){
        Portal portal = new Portal("http://www.arcgis.com");
        final PortalItem portalItem = new PortalItem(portal, "883cedb8c9fe4524b64d47666ed234a7");
        mLayer = new FeatureLayer(portalItem,0);
        mLayer.loadAsync();
        mLayer.addDoneLoadingListener(new Runnable() {
            @Override public void run() {
                if (mLayer.getLoadStatus() == LoadStatus.LOADED) {
                    Viewpoint viewpoint = new Viewpoint(mLayer.getFullExtent());
                    map.setInitialViewpoint(viewpoint);
                     // *** ADD ***
                    map.getOperationalLayers().add(mLayer);
                    mMapView.setMap(map);
                }
            }
        });
    }
    
  3. Finally, rather than setting the Map to the MapView in the setUpMap() method, pass the map to the addLayer(map).

    ArcGISMap map = new ArcGISMap(basemapType, latitude, longitude, levelOfDetail);
    addLayer(map);
    

Congratulations, you're done!

Your map and layer should load and display with the viewpoint centered on the Los Angeles area and points identifying the trailheads.

Challenge

Use your portal item

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

In Step 6 above, 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-up 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

Create a ServiceFeatureTable with this URL:

ServiceFeatureTable table =
                new ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0");

Create the FeatureLayer from the URL

// Create the layer from the FeatureTable
FeatureLayer layerFromTable = new FeatureLayer(table);

Add the layer to the map

map.getOperationalLayers().add(layerFromTable);

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.