Overview

You will learn: how to add feature layers to a 2D map.

With the ArcGIS Runtime SDK for Java, it is easy to create a mapping app that displays a basemap and layer that is a hosted feature service. ArcGIS Online hosts a number of tile and vector tile basemaps that you can choose from, including streets, satellite, streets-vector, and streets-navigation-vector. You can also add your own datasets by adding feature layers. These layers are hosted by ArcGIS Online or by other ArcGIS servers. By default, feature layers will draw with pre-set symbols to fill the extent of the map, but you can customize this by setting your own styles with a renderer or using a SQL expression to limit the data retrieved.

In this lab, you will build a simple mapping app with a basemap and a feature layer that represents trailheads near Los Angeles. The layer is hosted on the Los Angeles GeoHub.

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-layers-to-a-map.

Imports

  1. Open the App.java file in src/main/java/com/arcgis/developers/labs. Then merge the following with the existing imports.

    import com.esri.arcgisruntime.data.ServiceFeatureTable;
    import com.esri.arcgisruntime.layers.FeatureLayer;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    

Add a layer to the map

  1. Add a private method addTrailheadsLayer that creates a ServiceFeatureTable to hold the trailhead data from an online feature service.

    private void addTrailheadsLayer() {
      if (mapView != null) {
        String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0";
        ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
      }
    }
    
  2. Create a FeatureLayer using the service feature table and add it to the map as an operational layer. This will display the features above the basemap.

     ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
     /* ** ADD ** */
     FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
     ArcGISMap map = mapView.getMap();
     map.getOperationalLayers().add(featureLayer);
    
  3. To change the view to center on the feature layer's features, set the map view's viewpoint to the feature layer's extent after the layer is done loading:

    map.getOperationalLayers().add(featureLayer);
    /* ** ADD ** */
    featureLayer.addDoneLoadingListener(() -> {
      if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
        mapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
      } else {
        featureLayer.getLoadError().getCause().printStackTrace();
      }
    });
    

Congratulations, you're done!

Your map should load and center on the Los Angeles area with a separate layer (feature layer) that shows the trailhead symbols.

Challenge

Change the feature data

Return to the data lab where you created the trailheads layer and add or remove trailhead locations. Rerun your app and see the layer update without any code changes.

Change symbology

Return to the data lab where you created the trailheads layer and change the symbol used for trailhead locations. Rerun your app and see the layer update without any code changes.