Identify graphics

View on GitHubSample viewer app

Display an alert message when a graphic is tapped.

Image of identify graphics

Use case

A user may wish to select a graphic on a map to view relevant information about it.

How to use the sample

Select a graphic to identify it. You will see an alert message displayed.

How it works

  1. Create a GraphicsOverlay and add it to the MapView.
  2. Add a Graphic along with a SimpleFillSymbol to the graphics overlay.
  3. Create a Point from the location tapped on the map view by the user with a DefaultMapViewOnTouchListener.onSingleTapConfirmed method.
  4. Identify the graphic on the map view with identifyGraphicsOverlayAsync(graphicsOverlay, screenPoint, tolerance, max results).

Relevant API

  • Graphic
  • GraphicsOverlay
  • MapView

Tags

graphics, identify

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
/* 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.identifygraphics;

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

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.PolygonBuilder;
import com.esri.arcgisruntime.geometry.SpatialReferences;
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.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.IdentifyGraphicsOverlayResult;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.util.ListenableList;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private GraphicsOverlay grOverlay;

  @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 a map with the Basemap Style topographic
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map to be displayed in this view
    mMapView.setMap(map);
    mMapView.setViewpoint(new Viewpoint(3.184710, -4.734690, 100000000));

    // set up gesture for interacting with the MapView
    MainActivity.MapViewTouchListener mMapViewTouchListener = new MainActivity.MapViewTouchListener(this, mMapView);
    mMapView.setOnTouchListener(mMapViewTouchListener);

    addGraphicsOverlay();
  }

  private void addGraphicsOverlay() {
    // create the polygon
    PolygonBuilder polygonGeometry = new PolygonBuilder(SpatialReferences.getWebMercator());
    polygonGeometry.addPoint(-20e5, 20e5);
    polygonGeometry.addPoint(20e5, 20.e5);
    polygonGeometry.addPoint(20e5, -20e5);
    polygonGeometry.addPoint(-20e5, -20e5);

    // create solid line symbol
    SimpleFillSymbol polygonSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, Color.YELLOW, null);
    // create graphic from polygon geometry and symbol
    Graphic graphic = new Graphic(polygonGeometry.toGeometry(), polygonSymbol);

    // create graphics overlay
    grOverlay = new GraphicsOverlay();
    // create list of graphics
    ListenableList<Graphic> graphics = grOverlay.getGraphics();
    // add graphic to graphics overlay
    graphics.add(graphic);
    // add graphics overlay to the MapView
    mMapView.getGraphicsOverlays().add(grOverlay);
  }

  /**
   * Override default gestures of the MapView
   */
  class MapViewTouchListener extends DefaultMapViewOnTouchListener {

    /**
     * Constructs a DefaultMapViewOnTouchListener with the specified Context and MapView.
     *
     * @param context the context from which this is being created
     * @param mapView the MapView with which to interact
     */
    public MapViewTouchListener(Context context, MapView mapView) {
      super(context, mapView);
    }

    /**
     * Override the onSingleTapConfirmed gesture to handle tapping on the MapView
     * and detected if the Graphic was selected.
     *
     * @param e the motion event
     * @return true if the listener has consumed the event; false otherwise
     */
    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
      // get the screen point where user tapped
      Point screenPoint = new Point((int) e.getX(), (int) e.getY());

      // identify graphics on the graphics overlay
      final ListenableFuture<IdentifyGraphicsOverlayResult> identifyGraphic = mMapView
          .identifyGraphicsOverlayAsync(grOverlay, screenPoint, 10.0, false, 2);

      identifyGraphic.addDoneListener(() -> {
        try {
          IdentifyGraphicsOverlayResult grOverlayResult = identifyGraphic.get();
          // get the list of graphics returned by identify graphic overlay
          List<Graphic> graphic = grOverlayResult.getGraphics();
          // get size of list in results
          int identifyResultSize = graphic.size();
          if (!graphic.isEmpty()) {
            // show a toast message if graphic was returned
            Toast.makeText(getApplicationContext(), "Tapped on " + identifyResultSize + " Graphic", Toast.LENGTH_SHORT)
                .show();
          }
        } catch (InterruptedException | ExecutionException ie) {
          ie.printStackTrace();
        }

      });

      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.