Skip to content
View on GitHubSample viewer app

Display a message when a graphic on the map 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

Tap on 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. Build a Graphic from a Polygon and a SimpleFillSymbol and add it to the graphics overlay.
  3. Listen for MapView.onSingleTapConfirmed to obtain the ScreenCoordinate where the user tapped.
  4. Identify the graphic on the map view using MapViewProxy.identify function while providing the graphicsOverlay, screenCoordinate, tolerance, maximumResults.

Relevant API

  • Graphic
  • GraphicsOverlay
  • IdentifyGraphicsOverlayResult
  • MapView
  • MapViewProxy

Tags

graphics, identify

Sample Code

IdentifyGraphicsViewModel.ktIdentifyGraphicsViewModel.ktMainActivity.ktIdentifyGraphicsScreen.kt
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
/* Copyright 2025 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.arcgismaps.sample.identifygraphics.components

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import androidx.compose.ui.unit.dp
import com.arcgismaps.Color
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.geometry.PolygonBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleRenderer
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.mapping.view.ScreenCoordinate
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch

class IdentifyGraphicsViewModel(app: Application) : AndroidViewModel(app) {

    // Create a polygon from a list of points
    private val polygon: Polygon = PolygonBuilder(SpatialReference.webMercator()).apply {
        addPoint(Point(x = -20e5, y = 20e5))
        addPoint(Point(x = 20e5, y = 20e5))
        addPoint(Point(x = 20e5, y = -20e5))
        addPoint(Point(x = -20e5, y = -20e5))
    }.toGeometry()

    // ArcGISMap displayed by the MapView using a topographic basemap.
    val arcGISMap = ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
        // Set an initial viewpoint with the polygon graphic centered on the screen.
        initialViewpoint = Viewpoint(
            center = polygon.extent.center,
            scale = 1.3e8
        )
    }

    // MapViewProxy enables identify operations from the ViewModel.
    val mapViewProxy = MapViewProxy()

    // Graphics overlay that holds the polygon graphic and a renderer.
    private val graphicsOverlay = GraphicsOverlay().apply {
        // Configure the graphics overlay with a renderer and add a graphic for the polygon.
        val polygonFillSymbol = SimpleFillSymbol(
            style = SimpleFillSymbolStyle.Solid,
            color = Color.yellow
        )

        renderer = SimpleRenderer(polygonFillSymbol)
        graphics.add(Graphic(geometry = polygon))
    }

    val graphicsOverlays = listOf(graphicsOverlay)

    // Dialog view model to present error messages.
    val messageDialogVM = MessageDialogViewModel()

    init {
        // Load the map and handle any load failures.
        viewModelScope.launch {
            arcGISMap.load().onFailure { error ->
                messageDialogVM.showMessageDialog(error)
            }
        }
    }

    /**
     * Called when single tap is detected on the map. Identifies graphics in [graphicsOverlay] near the
     * [screenCoordinate] using [mapViewProxy], then shows the number of identified graphics in an
     * alert dialog.
     */
    fun identifyGraphics(screenCoordinate: ScreenCoordinate) {
        viewModelScope.launch {
            mapViewProxy.identify(
                graphicsOverlay = graphicsOverlay,
                screenCoordinate = screenCoordinate,
                tolerance = 12.dp,
                returnPopupsOnly = false,
                maximumResults = 10
            ).onSuccess { identifyResult ->
                val count = identifyResult.graphics.size
                val message = if (count > 0) {
                    "Tapped on $count graphic(s)."
                } else {
                    "No graphics at tapped location."
                }
                // Show an alert dialog with the identify result.
                messageDialogVM.showMessageDialog(
                    title = "Identify Result",
                    description = message
                )
            }.onFailure { error ->
                messageDialogVM.showMessageDialog(error)
            }
        }
    }
}

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