Identify layers

View inJavaKotlinView on GitHubSample viewer app

Identify features in all layers in a map.

Image of identify layers

Use case

IdentifyLayers allows users to tap on a map, returning features at that location across multiple layers. Because some layer types have sublayers, the sample recursively counts results for sublayers within each layer.

How to use the sample

Tap to identify features. An alert will show all layers with features under the tapped location, as well as the number of features.

How it works

  1. The tapped position is passed to MapView.identifyLayersAsync
  2. For each IdentifyLayerResult in the results, features are counted.
    • Note: there is one identify result per layer with matching features; if the feature count is 0, that means a sublayer contains the matching features.

Relevant API

  • IdentifyLayerResult
  • IdentifyLayerResult.getSublayerResults
  • MapView.identifyLayersAsync

Additional information

The GeoView supports two methods of identify: identifyLayerAsync(), which identifies features within a specific layer and identifyLayersAsync(), which identifies features for all layers in the current view.

Tags

identify, recursion, recursive, sublayers

Sample Code

MainActivity.java
Use dark colors for code blocksCopy
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright 2017 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.sample.identifylayers;

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

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

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.IdentifyLayerResult;
import com.esri.arcgisruntime.mapping.view.MapView;

public class MainActivity extends AppCompatActivity {

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

  private ArcGISMapImageLayer mMapImageLayer;
  private MapView mMapView;

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

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    mMapView = findViewById(R.id.mapView);

    // create an instance of a map
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

    // map image layer
    mMapImageLayer = new ArcGISMapImageLayer(getString(R.string.world_cities));
    mMapImageLayer.addDoneLoadingListener(() -> {
      // hide Continent and World layers
      mMapImageLayer.getSubLayerContents().get(1).setVisible(false);
      mMapImageLayer.getSubLayerContents().get(2).setVisible(false);
    });

    map.getOperationalLayers().add(mMapImageLayer);

    // feature table
    FeatureTable featureTable = new ServiceFeatureTable(getString(R.string.damage_assessment));

    // feature layer
    FeatureLayer featureLayer = new FeatureLayer(featureTable);

    // add feature layer add to the operational layers
    map.getOperationalLayers().add(featureLayer);

    // set initial viewpoint to a specific region
    mMapView.setViewpoint(
        new Viewpoint(new Point(-10977012.785807, 4514257.550369, SpatialReference.create(3857)), 68015210));

    // assign map to the map view
    mMapView.setMap(map);

    // add a listener to detect taps on the map view
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
      @Override public boolean onSingleTapConfirmed(MotionEvent e) {
        android.graphics.Point screenPoint = new android.graphics.Point(Math.round(e.getX()),
            Math.round(e.getY()));
        identifyResult(screenPoint);
        return true;
      }
    });
  }

  /**
   * Performs an identify on layers at the given screenpoint and calls handleIdentifyResults(...) to process them.
   *
   * @param screenPoint in Android graphic coordinates.
   */
  private void identifyResult(android.graphics.Point screenPoint) {

    final ListenableFuture<List<IdentifyLayerResult>> identifyLayerResultsFuture = mMapView
        .identifyLayersAsync(screenPoint, 12, false, 10);

    identifyLayerResultsFuture.addDoneListener(new Runnable() {
      @Override public void run() {
        try {
          List<IdentifyLayerResult> identifyLayerResults = identifyLayerResultsFuture.get();
          handleIdentifyResults(identifyLayerResults);
        } catch (InterruptedException | ExecutionException e) {
          Log.e(TAG, "Error identifying results: " + e.getMessage());
        }
      }
    });
  }

  /**
   * Processes identify results into a string which is passed to showAlertDialog(...).
   *
   * @param identifyLayerResults a list of identify results generated in identifyResult().
   */
  private void handleIdentifyResults(List<IdentifyLayerResult> identifyLayerResults) {
    StringBuilder message = new StringBuilder();
    int totalCount = 0;
    for (IdentifyLayerResult identifyLayerResult : identifyLayerResults) {
      int count = geoElementsCountFromResult(identifyLayerResult);
      String layerName = identifyLayerResult.getLayerContent().getName();
      message.append(layerName).append(": ").append(count);

      // add new line character if not the final element in array
      if (!identifyLayerResult.equals(identifyLayerResults.get(identifyLayerResults.size() - 1))) {
        message.append("\n");
      }
      totalCount += count;
    }

    // if any elements were found show the results, else notify user that no elements were found
    if (totalCount > 0) {
      showAlertDialog(message);
    } else {
      Toast.makeText(this, "No element found", Toast.LENGTH_SHORT).show();
      Log.i(TAG, "No element found.");
    }
  }

  /**
   * Gets a count of the GeoElements in the passed result layer.
   *
   * @param result from a single layer.
   * @return the total count of GeoElements.
   */
  private int geoElementsCountFromResult(IdentifyLayerResult result) {
    // create temp array
    List<IdentifyLayerResult> tempResults = new ArrayList<>();
    tempResults.add(result);

    // using Depth First Search approach to handle recursion
    int count = 0;
    int index = 0;

    while (index < tempResults.size()) {
      // get the result object from the array
      IdentifyLayerResult identifyResult = tempResults.get(index);

      // update count with geoElements from the result
      count += identifyResult.getElements().size();

      // if sublayer has any results, add result objects in the tempResults array after the current result
      if (identifyResult.getSublayerResults().size() > 0) {
        tempResults.add(identifyResult.getSublayerResults().get(index));
      }

      // update the count and repeat
      index += 1;
    }
    return count;
  }

  /**
   * Shows message in an AlertDialog.
   *
   * @param message contains identify results processed into a string.
   */
  private void showAlertDialog(StringBuilder message) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set title
    alertDialogBuilder.setTitle("Number of elements found");

    // set dialog message
    alertDialogBuilder
        .setMessage(message)
        .setCancelable(false)
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int id) {
          }
        });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show the alert dialog
    alertDialog.show();
  }

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

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

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

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