Identify graphics

View on GitHub

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

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. Add a Graphic along with a SimpleFillSymbol to the graphics overlay.
  3. Use the MapView.onSingleTapGesture(perform:) to get the screen point where a user tapped.
  4. Identify the graphic on the map view with MapViewProxy.identify(on:screenPoint:tolerance:returnPopupsOnly:maximumResults:).

Relevant API

  • Graphic
  • GraphicsOverlay
  • IdentifyGraphicsOverlayResult
  • MapView

Tags

graphics, identify

Sample Code

IdentifyGraphicsView.swift
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
// Copyright 2023 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct IdentifyGraphicsView: View {
    /// A map with a topographic basemap.
    @State private var map = Map(basemapStyle: .arcGISTopographic)

    /// A graphics overlay containing a yellow polygon graphic.
    @State private var graphicsOverlay = {
        let graphicsOverlay = GraphicsOverlay()

        // Create a polygon from a list of points.
        let polygon = Polygon(points: [
            Point(x: -20e5, y: 20e5),
            Point(x: 20e5, y: 20e5),
            Point(x: 20e5, y: -20e5),
            Point(x: -20e5, y: -20e5)
        ])

        // Create a graphic of the polygon and add it to the overlay.
        let polygonGraphic = Graphic(geometry: polygon)
        graphicsOverlay.addGraphic(polygonGraphic)

        // Create a renderer using a simple fill symbol.
        let polygonSymbol = SimpleFillSymbol(style: .solid, color: .yellow)
        graphicsOverlay.renderer = SimpleRenderer(symbol: polygonSymbol)

        return graphicsOverlay
    }()

    /// The tap location's screen point used in the identify operation.
    @State private var tapScreenPoint: CGPoint?

    /// A Boolean value indicating whether to show identify result alert.
    @State private var isShowingIdentifyResultAlert = false

    /// The message shown in the identify result alert.
    @State private var identifyResultMessage = "" {
        didSet { isShowingIdentifyResultAlert = identifyResultMessage.isNotEmpty }
    }

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        MapViewReader { mapViewProxy in
            MapView(map: map, graphicsOverlays: [graphicsOverlay])
                .onSingleTapGesture { screenPoint, _ in
                    tapScreenPoint = screenPoint
                }
                .task(id: tapScreenPoint) {
                    guard let tapScreenPoint else { return }

                    do {
                        // Identify the screen point on the graphics overlay using the proxy.
                        // Use `identifyGraphicsOverlays` instead if you need to identify on
                        // all the graphics overlay present in the map view.
                        let identifyResult = try await mapViewProxy.identify(
                            on: graphicsOverlay,
                            screenPoint: tapScreenPoint,
                            tolerance: 12
                        )

                        // Display an alert if any graphics were found at the screen point.
                        if identifyResult.graphics.isNotEmpty {
                            identifyResultMessage = "Tapped on \(identifyResult.graphics.count) graphic(s)."
                        }
                    } catch {
                        // Show an error alert for an error thrown while identifying.
                        self.error = error
                    }

                    self.tapScreenPoint = nil
                }
                .alert(
                    "Identify Result",
                    isPresented: $isShowingIdentifyResultAlert,
                    actions: {},
                    message: { Text(identifyResultMessage) }
                )
                .errorAlert(presentingError: $error)
        }
    }
}

private extension Collection {
    /// A Boolean value indicating whether the collection is not empty.
    var isNotEmpty: Bool {
        !self.isEmpty
    }
}

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