Feature layer show attributes

View on GitHub
Sample viewer app

Return all loaded features from a query to show all attributes.

Feature Layer Show Attributes App

Use case

Attributes can be used to help describe the objects represented by a feature. A geologist might use an attribute to describe the rock type of polygons representing surface geology. Archaeologists might use attributes to record the stratigraphic layer of finds represented as point features.

How to use the sample

Tap on a feature to see its attributes in a callout.

How it works

  1. Create an instance of a ServiceFeatureTable.
  2. Identify selected features with mMapView.identifyLayerAsync(...) and pass in the feature layer and tapped location to get all of the attributes for the feature at that location.
  3. Get the IdentifyLayerResult and iterate through the results to display each attribute in a callout.

Relevant API

  • Feature
  • FeatureLayer
  • FeatureQueryResult
  • ServiceFeatureTable

Tags

features, layers, query, attributes

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright 2016 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.featurelayershowattributes;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.GeoElement;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Callout;
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 MapView mMapView;
  private Callout mCallout;

  private ServiceFeatureTable mServiceFeatureTable;

  @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);

    // inflate MapView from layout
    mMapView = findViewById(R.id.mapView);
    // create an ArcGISMap with a topographic basemap
    final ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the ArcGISMap to the MapView
    mMapView.setMap(map);
    // set a viewpoint
    mMapView.setViewpoint(new Viewpoint(34.057386, -117.191455, 100000000));
    // get the callout that shows attributes
    mCallout = mMapView.getCallout();
    // create the service feature table
    mServiceFeatureTable = new ServiceFeatureTable(getResources().getString(R.string.sample_service_url));
    // create the feature layer using the service feature table
    final FeatureLayer featureLayer = new FeatureLayer(mServiceFeatureTable);
    // add the layer to the map
    map.getOperationalLayers().add(featureLayer);

    // set an on touch listener to listen for click events
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
      @Override
      public boolean onSingleTapConfirmed(MotionEvent e) {
        // remove any existing callouts
        if (mCallout.isShowing()) {
          mCallout.dismiss();
        }
        // get the point that was clicked and convert it to a point in map coordinates
        final Point screenPoint = new Point(Math.round(e.getX()), Math.round(e.getY()));
        // create a selection tolerance
        int tolerance = 10;
        // use identifyLayerAsync to get tapped features
        final ListenableFuture<IdentifyLayerResult> identifyLayerResultListenableFuture = mMapView
            .identifyLayerAsync(featureLayer, screenPoint, tolerance, false, 1);
        identifyLayerResultListenableFuture.addDoneListener(() -> {
          try {
            IdentifyLayerResult identifyLayerResult = identifyLayerResultListenableFuture.get();
            // create a textview to display field values
            TextView calloutContent = new TextView(getApplicationContext());
            calloutContent.setTextColor(Color.BLACK);
            calloutContent.setSingleLine(false);
            calloutContent.setVerticalScrollBarEnabled(true);
            calloutContent.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
            calloutContent.setMovementMethod(new ScrollingMovementMethod());
            calloutContent.setLines(5);
            for (GeoElement element : identifyLayerResult.getElements()) {
              Feature feature = (Feature) element;
              // create a map of all available attributes as name value pairs
              Map<String, Object> attr = feature.getAttributes();
              Set<String> keys = attr.keySet();
              for (String key : keys) {
                Object value = attr.get(key);
                // format observed field value as date
                if (value instanceof GregorianCalendar) {
                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
                  value = simpleDateFormat.format(((GregorianCalendar) value).getTime());
                }
                // append name value pairs to text view
                calloutContent.append(key + " | " + value + "\n");
              }
              // center the mapview on selected feature
              Envelope envelope = feature.getGeometry().getExtent();
              mMapView.setViewpointGeometryAsync(envelope, 200);
              // show callout
              mCallout.setLocation(envelope.getCenter());
              mCallout.setContent(calloutContent);
              mCallout.show();
            }
          } catch (Exception e1) {
            Log.e(getResources().getString(R.string.app_name), "Select feature failed: " + e1.getMessage());
          }
        });
        return super.onSingleTapConfirmed(e);
      }
    });

  }

  @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.