Add a point, line, and polygon

Learn how to display point, line, and polygon graphics in a map.

add a point line and polygon

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.

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.

Steps

Open the Xcode project

  1. To start the tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the .xcodeproj file in Xcode.

  3. If you downloaded the solution project, set your API key.

Add a graphics overlay

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.

  1. In Xcode, in the Project navigator, click ViewController.swift.

  2. 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 blocks
    14 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()
    
        }
    
    }

Add a point graphic

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.

  1. 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 blocks
    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 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)
    
    
    
        }
    
    Expand
  2. 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 blocks
    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 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)
    
    Expand
  3. Press Command + R to run the app.

You should see a point graphic in Point Dume State Beach.

Add a line graphic

A line graphic is created using a polyline and a line symbol. A polyline is defined as a sequence of points.

  1. Create an AGSPolyline and an AGSSimpleLineSymbol. To create the AGSPolyline, provide an array of AGSPoints.

    Expand
    Use dark colors for code blocks
    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 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)
    
    
    Expand
  2. 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 blocks
    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 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)
    
    Expand
  3. Press Command + R to run the app.

You should see a point and line graphic along Westward Beach.

Add a polygon graphic

A 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.

  1. Create an AGSPolygon and an AGSSimpleFillSymbol. To create the AGSPolygon, provide an array of AGSPoints.

    Expand
    Use dark colors for code blocks
    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 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))
    
    
    Expand
  2. 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 blocks
    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 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)
    
    Expand
  3. Press Command + R to run the app.

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:

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