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

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

How it works

  1. Create an AGSGraphicsOverlay and add it to the AGSMapView.
  2. Add an AGSGraphic along with an AGSSimpleFillSymbol to the graphics overlay.
  3. Use AGSGeoViewTouchDelegate.geoView(_:didTapAtScreenPoint:mapPoint:) to get the mapPoint where a user tapped on the map.
  4. Identify the graphic tapped on the map view with AGSMapView.identify(_:screenPoint:tolerance:returnPopupsOnly:maximumResults:completion:).

Relevant API

  • AGSGraphic
  • AGSGraphicsOverlay
  • AGSMapView

Tags

graphics, identify

Sample Code

GOIdentifyViewController.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
// 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.

import UIKit
import ArcGIS

class GOIdentifyViewController: UIViewController, AGSGeoViewTouchDelegate {
    @IBOutlet private weak var mapView: AGSMapView!

    private var map: AGSMap!
    private let graphicsOverlay = AGSGraphicsOverlay()

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["GOIdentifyViewController"]

        // initialize the map with topographic basemap
        self.map = AGSMap(basemapStyle: .arcGISTopographic)

        // call the method to add a graphics to the map view
        // will be using this graphic to test identify
        self.addGraphicsOverlay()

        // assign the map to the map view's map object
        self.mapView.map = self.map

        // add self as the touch delegate of the mapview
        // we will be using a method on the delegate to know
        // when the user tapped on the map view
        self.mapView.touchDelegate = self
    }

    func addGraphicsOverlay() {
        // polygon graphic
        let polygonGeometry = AGSPolygonBuilder(spatialReference: .webMercator())
        polygonGeometry.addPointWith(x: -20e5, y: 20e5)
        polygonGeometry.addPointWith(x: 20e5, y: 20e5)
        polygonGeometry.addPointWith(x: 20e5, y: -20e5)
        polygonGeometry.addPointWith(x: -20e5, y: -20e5)
        let polygonSymbol = AGSSimpleFillSymbol(style: AGSSimpleFillSymbolStyle.solid, color: .yellow, outline: nil)
        let polygonGraphic = AGSGraphic(geometry: polygonGeometry.toGeometry(), symbol: nil, attributes: nil)

        // assign the renderer
        self.graphicsOverlay.renderer = AGSSimpleRenderer(symbol: polygonSymbol)
        // add the polygon graphic
        self.graphicsOverlay.graphics.add(polygonGraphic)
        // add the graphics overlay to the map view
        self.mapView.graphicsOverlays.add(self.graphicsOverlay)
    }

    // MARK: - AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        // use the following method to identify graphics in a specific graphics overlay
        // otherwise if you need to identify on all the graphics overlay present in the map view
        // use `identifyGraphicsOverlaysAtScreenCoordinate:tolerance:maximumGraphics:completion:` method provided on map view
        let tolerance: Double = 12

        self.mapView.identify(self.graphicsOverlay, screenPoint: screenPoint, tolerance: tolerance, returnPopupsOnly: false, maximumResults: 10) { [weak self] (result: AGSIdentifyGraphicsOverlayResult) in
            if let error = result.error {
                print("error while identifying :: \(error.localizedDescription)")
            } else {
                // if a graphics is found then show an alert
                if !result.graphics.isEmpty {
                    self?.presentAlert(message: "Tapped on graphic")
                }
            }
        }
    }
}

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