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 can load the individual layers directly from ArcGIS Online and build your map with code. To do so, you need to access each item by ID. You can find an item's ID by browsing to the item on ArcGIS Online and identifying it at the end of the url, e.g. https://www.arcgis.com/home/item.html?id=2e4b3df6ba4b44969a3bc9827de746b3. You can also publish your own feature layer to ArcGIS Online using the Create a new dataset 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 in the device simulator.
  • You may be required to sync the project dependencies.

Steps

View the Trailheads layer by ID

  1. In your browser, go to the Trailheads Styled layer on ArcGIS Online and find the item ID at the end of the url. It should be 2e4b3df6ba4b44969a3bc9827de746b3.

Add a new class member

  1. In Android Studio, open the file app > java > {your.package.name} > MainActivity.java. Define a new class-level FeatureLayer variable and name it mFeatureLayer to hold a reference to the feature layer that you will create in this lab.

    private FeatureLayer mFeatureLayer;
    
  2. In the setUpMap() method, change the Basemap.Type to TOPOGRAPHIC.

    Basemap.Type basemapType = Basemap.Type.TOPOGRAPHIC;
    
  3. 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) {
        }
    

Load the portal item

  1. Add to the new method a string variable to hold the item ID of the layer to add.

        String itemID = "2e4b3df6ba4b44969a3bc9827de746b3";
    
  2. 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");
    
  3. Create a variable to store an ArcGIS Online PortalItem. The PortalItem represents content stored in ArcGIS Online - in this case, the Trailheads Styled portal item (feature layer).

        final PortalItem portalItem = new PortalItem(portal, itemID);
    

Create the layer from the portal item

  1. Create the layer using the portal item and its layer index. 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.

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

Add the layer to the map

  1. Ensure the layer is loaded and then add the layer to the map's operational layers.

        mLayer.addDoneLoadingListener(new Runnable() {
            // *** ADD ***
            @Override public void run() {
                if (mFeatureLayer.getLoadStatus() == LoadStatus.LOADED) {
                    map.getOperationalLayers().add(mFeatureLayer);
                }
            }
    
  2. Find the setupMap() method and update it to call the new addLayer() function after the map is added to the map view.

            mMapView.setMap(map);
            // *** ADD ***
            addLayer(map);
    

Add the Trails and Parks and Open Space layers by id

  1. Use the same code pattern in step 5 and 7 to load two more layers into the map. Go to the Trails Styled and the Parks and Open Space Styled layers to find the item IDs and then add the code to add them to the map.

  2. Run your code to view the map. You should see all three layers.

Congratulations, you're done!

Your app should display a map with the trailheads centered on the Santa Monica Mountains. Compare your solution with our completed solution project.

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.