Overview

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

With ArcGIS Runtime SDK for Java, it's easy to create a mapping app that displays a basemap and one or more layers. ArcGIS Online hosts several tile and vector tile basemaps that you can use in your map (2D) or scene (3D). Feature layers allow you to add datasets hosted online (with ArcGIS Online or other ArcGIS servers) or stored locally (in a runtime geodatabase, for example). By default, feature layers will draw with pre-set symbols to fill the extent of 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 create a new scene with an imagery basemap, add a feature layer that represents trails near Los Angeles, and display the scene in your app.

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

Imports

  1. Open the App.java file in src/main/java/com/arcgis/developers/labs. Then replace any Esri imports with ones below.
    import com.esri.arcgisruntime.data.ServiceFeatureTable;
    import com.esri.arcgisruntime.layers.FeatureLayer;
    import com.esri.arcgisruntime.mapping.ArcGISScene;
    import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
    import com.esri.arcgisruntime.mapping.Basemap;
    import com.esri.arcgisruntime.mapping.view.Camera;
    import com.esri.arcgisruntime.mapping.view.SceneView;
    

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-3d-scene.

Modify the project for a scene (3D)

  1. Replace MapView with a SceneView. This will now display the map as a globe, which is referred to as a scene. All references to mapView should now be sceneView.

     private SceneView sceneView;
    
     //...
    
     sceneView = new SceneView();
    
     // ...
    
     @Override
     public void stop() {
       if (sceneView != null) {
         sceneView.dispose();
       }
     }
    
  2. Update method from setupMap to setupScene. This will replace ArcGISMap with a ArcGISScene.

    private void setupScene() {
      if (sceneView != null) {
        ArcGISScene scene = new ArcGISScene(Basemap.createStreets());
        sceneView.setArcGISScene(scene);
      }
    }
    
  3. Set the scene's viewpoint to Los Angeles using a Camera.

     sceneView.setArcGISScene(scene);
     /* ** ADD ** */
     Camera camera = new Camera(
       33.950896,
       -118.525341,
       16000.0,
       0.0,
       50.0,
       0.0);
     sceneView.setViewpointCamera(camera);
    
  4. Update the call to the method setupMap as well.

     sceneView = new SceneView();
     setupScene();
    

Add a new feature layer

  1. Add a new private method addTrailheadsLayer that creates then adds a feature layer to the scene using the Los Angeles Trails service.

     private void addTrailheadsLayer() {
       if (sceneView != null) {
         String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0";
         ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
         FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
         sceneView.getArcGISScene().getOperationalLayers().add(featureLayer);
       }
     }
    
  2. Add addTrailheadsLayer to the start method.

     setupScene();
     /* ** ADD ** */
     addTrailheadsLayer();
    

Set the elevation source for the scene

  1. Add a new private method addElevationData that creates an elevation service, provided by Esri, and add it to the scene.

     private void addElevationData() {
       ArcGISTiledElevationSource elevationSource = new ArcGISTiledElevationSource(
         "http://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer");
       sceneView.getArcGISScene().getBaseSurface().getElevationSources().add(elevationSource);
     }
    
  2. Add addElevationData to the start method.

     addTrailheadsLayer();
     /* ** ADD ** */
     addElevationData();
    

Congratulations, you're done!

Run your app to test your code. When the app opens, you should see imagery of Los Angeles appear with trail lines displayed on top. The scene is rendered in 3D with the elevation data that gives accurate detail to the terrain.

Checkout and compare with our completed solution project.

Challenge

Customize your viewpoint with the camera

You can obtain the viewpoint camera from the scene. Using a ViewpointChangedListener observe how camera settings change as you move around in the map. Modify your code to display a different viewpoint with your own custom camera settings.

Add additional feature layers

Visit ArcGIS Online and find additional interesting park datasets to load into the app. Here are a couple suggestions:

  • Trailheads (points): https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0
  • Parks (polygons): https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0

Filter features in the trails layer

Sometimes you want your layer to show a subset of the data from a service. A definition expression lets you define attribute criteria that restrict the features for a layer. Consult the service info for the Trailheads data source and construct an expression for the layer's DefinitionExpression property that will show only those trailheads without parking.