Add a point, line, and polygon

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

add a point line and polygon

You typically use 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 , , and .

In this tutorial, you display points, lines, and polygons on a map as .

To learn how to display data from , see the Add a feature layer tutorial.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  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, get an access token and set the API key.

Add a graphics overlay

A is a container for . It is used with a to display graphics on a . You can add more than one to a . Graphics overlays are displayed on top of all the other .

  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
    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
    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 is created using a and a . A point is defined with x and y coordinates, and a . 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
    37 38 39 40 41 42 43 44 45 46 47 48 49 50
    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
    46 47 48 49 50
    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 is created using a and a . 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
    50 51 52 53 54 55 56 57 58 59 60 61 62
    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
    61 62 63 64 65
    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 is created using a and a . 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
    65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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
    78 79 80 81 82
    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.

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close