Scene layer selection

View on GitHub
Sample viewer app

Identify features in a scene to select.

Image of scene layer selection

Use case

You can select features to visually distinguish them with a selection color or highlighting. This can be useful to demonstrate the physical extent or associated attributes of a feature, or to initiate another action such as centering that feature in the scene view.

How to use the sample

Tap a building in the scene layer to select it. Deselect buildings by tapping away from the buildings.

How it works

  1. Create an ArcGISSceneLayer passing in the URL to a scene layer service.
  2. Use sceneView.setOnTouchListener(...) to get the screen tap location screenPoint.
  3. Call sceneView.identifyLayersAsync(sceneLayer, screenPoint, tolerance, false, 1) to identify features in the scene.
  4. From the resulting IdentifyLayerResult, get the list of identified GeoElements with result.getElements().
  5. Get the first element in the list, checking that it is a feature, and call sceneLayer.selectFeature(feature) to select it.

About the data

This sample shows a Berlin, Germany Scene hosted on ArcGIS Online.

Relevant API

  • ArcGISSceneLayer
  • Scene
  • SceneView

Tags

3D, Berlin, buildings, identify, model, query, search, select

Sample Code

MainActivity.java
                                                                                                                                             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright 2018 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgisruntime.scenelayerselection;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.layers.ArcGISSceneLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.GeoElement;
import com.esri.arcgisruntime.mapping.Surface;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.DefaultSceneViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;
import com.esri.arcgisruntime.mapping.view.SceneView;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private SceneView mSceneView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create a scene and add a basemap to it
    ArcGISScene scene = new ArcGISScene();
    scene.setBasemap(Basemap.createTopographic());

    // set the scene to the scene view
    mSceneView = findViewById(R.id.sceneView);
    mSceneView.setScene(scene);

    // add base surface with elevation data
    Surface surface = new Surface();
    final String elevationService = getString(R.string.world_elevation_url);
    surface.getElevationSources().add(new ArcGISTiledElevationSource(elevationService));
    scene.setBaseSurface(surface);

    // add a scene layer of Brest buildings to the scene
    final String buildings = getString(R.string.brest_buildings);
    ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(buildings);
    scene.getOperationalLayers().add(sceneLayer);

    // add a camera and initial camera position
    Camera camera = new Camera(48.378, -4.494, 200, 345, 65, 0);
    mSceneView.setViewpointCamera(camera);

    // zoom to the layer's extent when loaded
    sceneLayer.addDoneLoadingListener(() -> {
      if (sceneLayer.getLoadStatus() == LoadStatus.LOADED) {

        // when the scene is clicked, identify the clicked feature and select it
        mSceneView.setOnTouchListener(new DefaultSceneViewOnTouchListener(mSceneView) {

          @Override public boolean onSingleTapConfirmed(MotionEvent motionEvent) {

            // clear any previous selection
            sceneLayer.clearSelection();

            android.graphics.Point screenPoint = new android.graphics.Point(Math.round(motionEvent.getX()),
                Math.round(motionEvent.getY()));
            // identify clicked feature
            ListenableFuture<IdentifyLayerResult> identify = mSceneView
                .identifyLayerAsync(sceneLayer, screenPoint, 10, false, 1);
            identify.addDoneListener(() -> {
              try {
                // get the identified result and check that it is a feature
                IdentifyLayerResult result = identify.get();
                List<GeoElement> geoElements = result.getElements();
                if (!geoElements.isEmpty()) {
                  Log.d(TAG, "geoelement not empty");
                  GeoElement geoElement = geoElements.get(0);
                  if (geoElement instanceof Feature) {
                    // select the feature
                    sceneLayer.selectFeature((Feature) geoElement);
                  }
                }
              } catch (InterruptedException | ExecutionException e) {
                String error = "Error while identifying layer result: " + e.getMessage();
                Log.e(TAG, error);
                Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
              }
            });
            return true;
          }
        });
      } else if (sceneLayer.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
        String error = "Error loading scene layer " + sceneLayer.getLoadStatus();
        Log.e(TAG, error);
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
      }
    });
  }

  @Override
  protected void onPause() {
    super.onPause();
    mSceneView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mSceneView.resume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mSceneView.dispose();
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.