Learn how to display point, line, and polygon graphics in a map.
You typically use graphics to display geographic data that is not connected to a database and that is not persisted, like highlighting a route between two locations, displaying a search buffer around a point, or tracking the location of a vehicle in real-time. Graphics are composed of a geometry, symbol, and attributes.
In this tutorial, you display points, lines, and polygons on a map as graphics.
To learn how to display data from data sources, see the Add a feature layer tutorial.
NoteFor more background information about the topics in this tutorial, visit Maps (2D), Graphics, and Data hosting in the Portal and data services guide.
PrerequisitesBefore starting this tutorial:
You need an ArcGIS Location Platform or ArcGIS Online account.
Your system meets the system requirements.
To start the tutorial, complete the Display a map tutorial or download and unzip the solution.
Open the .xcodeproj
file in Xcode.
If you downloaded the solution, get an access token and set the API key.
An API Key gives your app access to secure resources used in this tutorial.
Go to the Create an API key tutorial to obtain a new API key access token using your ArcGIS Location Platform or ArcGIS Online account. Ensure that the following privilege is enabled: Location services > Basemaps > Basemap styles service. Copy the access token as it will be used in the next step.
In Xcode, in the Project Navigator, click AppDelegate.swift.
In the editor, set the APIKey
property on the AGSArcGISRuntimeEnvironment
with your access token.
AppDelegate.swift
Expand
Use dark colors for code blocks1
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
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AGSArcGISRuntimeEnvironment.apiKey = "YOUR_ACCESS_TOKEN"
return true
}
Warning
The access token is stored directly in the code as a convenience for this tutorial. Storing access tokens in the source code is not best practice.
A graphics overlay is a container for graphics. It is used with a map view to display graphics on a map. You can add more than one graphics overlay to a map view. Graphics overlays are displayed on top of all the other layers.
In Xcode, in the Project navigator, click ViewController.swift.
Create a new method named addGraphics
. Create an AGSGraphicsOverlay
to display point, line, and polygon graphics, and add it to the mapView
's collection of graphics overlays. Call the addGraphics()
method from the ViewController
's viewDidLoad()
method.
ViewController.swift
Expand
Use dark colors for code blocks14 14 14 14 14 14 14 14 14 14 14 14 14 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 42 42 42 42 42 42 42 42 42 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 45 46 47 48 49 50 51 52 53 54 55 56
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.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
import UIKit
import ArcGIS
class ViewController: UIViewController {
@IBOutlet weak var mapView: AGSMapView!
private func setupMap() {
let map = AGSMap(
basemapStyle: .arcGISTopographic
)
mapView.map = map
mapView.setViewpoint(
AGSViewpoint(
latitude: 34.02700,
longitude: -118.80500,
scale: 72_000
)
)
}
private func addGraphics() {
let graphicsOverlay = AGSGraphicsOverlay()
mapView.graphicsOverlays.add(graphicsOverlay)
}
override func viewDidLoad() {
super.viewDidLoad()
setupMap()
addGraphics()
}
}
A point graphic is created using a point and a marker symbol. A point is defined with x and y coordinates, and a spatial reference. For latitude and longitude coordinates, the spatial reference is WGS84.
Create an AGSPoint
and an AGSSimpleMarkerSymbol
. To create the AGSPoint
, provide longitude (x) and latitude (y) coordinates, and an AGSSpatialReference
. Use the AGSSpatialReference.wgs84()
convenience method.
Expand
Use dark colors for code blocks36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 37 38 39 40 41 42 43 44 45 46 47 47 47 47 47 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 50 50 50 50 50 50 50 50 50 50 50 50
Add line. Add line. Add line. Add line. Add line.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
private func addGraphics() {
let graphicsOverlay = AGSGraphicsOverlay()
mapView.graphicsOverlays.add(graphicsOverlay)
let point = AGSPoint(x: -118.80657463861, y: 34.0005930608889, spatialReference: .wgs84())
let pointSymbol = AGSSimpleMarkerSymbol(style: .circle, color: .orange, size: 10.0)
pointSymbol.outline = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 2.0)
}
You can also create AGSPoint
s with latitude and longitude using the AGSPointMakeWGS84()
convenience function. Notice that the order of the coordinates passed into this function is y,x (latitude,longitude):
Use dark colors for code blocks Copy
1
AGSPointMakeWGS84(34.0005930608889, -118.80657463861)
Create an AGSGraphic
with the point
and pointSymbol
. Display the AGSGraphic
by adding it to the graphicsOverlay
's graphics
collection.
Expand
Use dark colors for code blocks45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 46 47 48 49 50 50 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 20 20 20 20 20 20 20 20 20 20 20 20
Add line. Add line. Add line.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
pointSymbol.outline = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 2.0)
let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
graphicsOverlay.graphics.add(pointGraphic)
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a point graphic in Point Dume State Beach.
Add a line graphicA line graphic is created using a polyline and a line symbol. A polyline is defined as a sequence of points.
Polylines have one or more distinct parts. Each part is a sequence of points. For a continuous line, you can use the AGSPolyline
constructor to create a polyline with just one part. To create a polyline with more than one part, use an AGSPolylineBuilder
.
Create an AGSPolyline
and an AGSSimpleLineSymbol
. To create the AGSPolyline
, provide an array of AGSPoint
s.
Expand
Use dark colors for code blocks49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 50 51 52 53 54 55 56 57 58 59 60 61 62 62 62 62 61 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 45 45 45 45 45 45 45 45 45 45 45 45
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.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
graphicsOverlay.graphics.add(pointGraphic)
let polyline = AGSPolyline(
points: [
AGSPoint(x: -118.821527826096, y: 34.0139576938577, spatialReference: .wgs84()),
AGSPoint(x: -118.814893761649, y: 34.0080602407843, spatialReference: .wgs84()),
AGSPoint(x: -118.808878330345, y: 34.0016642996246, spatialReference: .wgs84())
]
)
let polylineSymbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3.0)
Create an AGSGraphic
with the polyline
and polylineSymbol
. Display the AGSGraphic
by adding it to the graphicsOverlay
's graphics
collection.
Expand
Use dark colors for code blocks60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 60 61 62 63 64 65 65 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 49 49 49 49 49 49 49 49 49 49 49 49
Add line. Add line. Add line.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
let polylineSymbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3.0)
let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
graphicsOverlay.graphics.add(polylineGraphic)
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a point and line graphic along Westward Beach.
Add a polygon graphicA polygon graphic is created using a polygon and a fill symbol. A polygon is defined as a sequence of points that describe a closed boundary.
Polygons have one or more distinct parts. Each part is a sequence of points describing a closed boundary. For a single area with no holes, you can use the AGSPolygon
constructor to create a polygon with just one part. To create a polygon with more than one part, use an AGSPolygonBuilder
.
Create an AGSPolygon
and an AGSSimpleFillSymbol
. To create the AGSPolygon
, provide an array of AGSPoint
s.
Expand
Use dark colors for code blocks64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 79 79 79 78 78 78 78 78 78 78 78 78 78 78 78 78
Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line.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
graphicsOverlay.graphics.add(polylineGraphic)
let polygon = AGSPolygon(
points: [
AGSPoint(x: -118.818984489994, y: 34.0137559967283, spatialReference: .wgs84()),
AGSPoint(x: -118.806796597377, y: 34.0215816298725, spatialReference: .wgs84()),
AGSPoint(x: -118.791432890735, y: 34.0163883241613, spatialReference: .wgs84()),
AGSPoint(x: -118.79596686535, y: 34.008564864635, spatialReference: .wgs84()),
AGSPoint(x: -118.808558110679, y: 34.0035027131376, spatialReference: .wgs84())
]
)
let polygonSymbol = AGSSimpleFillSymbol(style: .solid, color: .orange, outline: AGSSimpleLineSymbol(style: .solid, color: .blue, width: 2.0))
Create an AGSGraphic
with the polygon
and polygonSymbol
. Display the AGSGraphic
by adding it to the graphicsOverlay
's graphics
collection.
Expand
Use dark colors for code blocks77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 78 79 80 81 82 82 82 82 82 82 82 82 82 82 82 82 82 82
Add line. Add line. Add line.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
let polygonSymbol = AGSSimpleFillSymbol(style: .solid, color: .orange, outline: AGSSimpleLineSymbol(style: .solid, color: .blue, width: 2.0))
let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
graphicsOverlay.graphics.add(polygonGraphic)
Press Command + R to run the app.
If you are using the Xcode simulator your system must meet these minimum requirements: macOS Big Sur 11.3, Xcode 13, iOS 13. If you are using a physical device, then refer to the system requirements.
You should see a point, line, and polygon graphic around Mahou Riviera in the Santa Monica Mountains.
What's next?Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4