Display an alert message when a graphic is tapped.
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
Create an AGSGraphicsOverlay and add it to the AGSMapView.
Add an AGSGraphic along with an AGSSimpleFillSymbol to the graphics overlay.
Use AGSGeoViewTouchDelegate.geoView(_:didTapAtScreenPoint:mapPoint:) to get the mapPoint where a user tapped on the map.
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
classGOIdentifyViewController: UIViewController, AGSGeoViewTouchDelegate{
@IBOutletprivateweakvar mapView: AGSMapView!
privatevar map: AGSMap!
privatelet graphicsOverlay =AGSGraphicsOverlay()
overridefuncviewDidLoad() {
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 basemapself.map =AGSMap(basemapStyle: .arcGISTopographic)
// call the method to add a graphics to the map view// will be using this graphic to test identifyself.addGraphicsOverlay()
// assign the map to the map view's map objectself.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 viewself.mapView.touchDelegate =self }
funcaddGraphicsOverlay() {
// polygon graphiclet 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 rendererself.graphicsOverlay.renderer =AGSSimpleRenderer(symbol: polygonSymbol)
// add the polygon graphicself.graphicsOverlay.graphics.add(polygonGraphic)
// add the graphics overlay to the map viewself.mapView.graphicsOverlays.add(self.graphicsOverlay)
}
// MARK: - AGSGeoViewTouchDelegatefuncgeoView(_geoView: AGSGeoView, didTapAtScreenPointscreenPoint: 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 viewlet tolerance: Double=12self.mapView.identify(self.graphicsOverlay, screenPoint: screenPoint, tolerance: tolerance, returnPopupsOnly: false, maximumResults: 10) { [weakself] (result: AGSIdentifyGraphicsOverlayResult) iniflet error = result.error {
print("error while identifying :: \(error.localizedDescription)")
} else {
// if a graphics is found then show an alertif!result.graphics.isEmpty {
self?.presentAlert(message: "Tapped on graphic")
}
}
}
}
}