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.

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 ContentView.swift.

  2. Create a class called Model that adopts the ObservableObject protocol. Within that class, create a GraphicsOverlay as a default value. Initialize the property with an empty initializer and return it. You'll edit this method at a later step to add all of the graphics to display.

    ContentView.swift
    Use dark colors for code blocks
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 27 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28
    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
    import SwiftUI
    import ArcGIS
    
    
    private class Model: ObservableObject {
        let graphicsOverlay: GraphicsOverlay = {
            let graphicsOverlay = GraphicsOverlay()
    
            return graphicsOverlay
    
        }()
    }
    
    
  3. Create a @StateObject of type Model called graphicsOverlayModel in the ContentView.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 28 28 28 28 28 29 30 31 32 33 34 35 36 37 38 38 38 38 38 38 38 39
    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
    struct ContentView: View {
    
        @State private var map: Map = {
            let map = Map(basemapStyle: .arcGISTopographic)
            map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000)
            return map
        }()
    
        @StateObject private var graphicsOverlayModel = Model()
    
    }
  4. In the body, update the map view initializer by adding the graphicsOverlays parameter. Pass in the graphicsOverlayModel's graphicOverlay property to add the graphics overlay created in the previous steps to the map view's list of graphics overlays.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 28 28 28 28 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    Change 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
    struct ContentView: View {
    
        @State private var map: Map = {
            let map = Map(basemapStyle: .arcGISTopographic)
            map.initialViewpoint = Viewpoint(latitude: 34.02700, longitude: -118.80500, scale: 72_000)
            return map
        }()
    
        @StateObject private var graphicsOverlayModel = Model()
    
        var body: some View {
    
            MapView(map: map, graphicsOverlays: [graphicsOverlayModel.graphicsOverlay])
    
        }
    
    }

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 a Point and a SimpleMarkerSymbol. To create the Point, provide longitude (x) and latitude (y) coordinates, and a SpatialReference. Use the SpatialReference.wgs84 convenience method. Create and style a SimpleMarkerSymbol.

    Expand
    Use dark colors for code blocks
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 24 25 26 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 28 29 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31
    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
    private class Model: ObservableObject {
        let graphicsOverlay: GraphicsOverlay = {
            let graphicsOverlay = GraphicsOverlay()
    
            let point = Point(x: -118.80657, y: 34.00059, spatialReference: .wgs84)
            let pointSymbol = SimpleMarkerSymbol(style: .circle, color: .orange, size: 10.0)
            pointSymbol.outline = SimpleLineSymbol(style: .solid, color: .blue, width: 2.0)
    
            return graphicsOverlay
    
        }()
    }
    
    Expand
  2. Create a Graphic with the point and pointSymbol. Display the Graphic by adding it to the graphicsOverlay using the GraphicsOverlay.addGraphic() method.

    Expand
    Use dark colors for code blocks
    23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 25 26 27 28 29 30 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
    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
            let point = Point(x: -118.80657, y: 34.00059, spatialReference: .wgs84)
            let pointSymbol = SimpleMarkerSymbol(style: .circle, color: .orange, size: 10.0)
            pointSymbol.outline = SimpleLineSymbol(style: .solid, color: .blue, width: 2.0)
    
            let pointGraphic = Graphic(geometry: point, symbol: pointSymbol)
    
            graphicsOverlay.addGraphic(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 a Polyline and a SimpleLineSymbol. To create the Polyline, provide an array of Point objects. Create and style a SimpleLineSymbol.

    Expand
    Use dark colors for code blocks
    29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 30 31 32 33 34 35 36 37 38 39 40 40 40 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
    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
            graphicsOverlay.addGraphic(pointGraphic)
    
            let polyline = Polyline(
                points: [
                    Point(x: -118.82152, y: 34.01395, spatialReference: .wgs84),
                    Point(x: -118.81489, y: 34.00806, spatialReference: .wgs84),
                    Point(x: -118.80887, y: 34.00166, spatialReference: .wgs84)
                ]
            )
            let polylineSymbol = SimpleLineSymbol(style: .solid, color: .blue, width: 3.0)
    
    
    Expand
  2. Create a Graphic with the polyline and polylineSymbol. Display the Graphic by adding it to the graphicsOverlay using the GraphicsOverlay.addGraphic() method.

    Expand
    Use dark colors for code blocks
    31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 32 33 34 35 36 37 38 39 40 41 42 43 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29
    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
            let polyline = Polyline(
                points: [
                    Point(x: -118.82152, y: 34.01395, spatialReference: .wgs84),
                    Point(x: -118.81489, y: 34.00806, spatialReference: .wgs84),
                    Point(x: -118.80887, y: 34.00166, spatialReference: .wgs84)
                ]
            )
            let polylineSymbol = SimpleLineSymbol(style: .solid, color: .blue, width: 3.0)
    
            let polylineGraphic = Graphic(geometry: polyline, symbol: polylineSymbol)
    
            graphicsOverlay.addGraphic(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 a Polygon and a SimpleFillSymbol. To create the Polygon, provide an array of Point objects. Create and style a SimpleFillSymbol.

    Expand
    Use dark colors for code blocks
    42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 43 44 45 46 47 48 49 50 51 52 53 54 55 55 55 55 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56
    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
            graphicsOverlay.addGraphic(polylineGraphic)
    
            let polygon = Polygon(
                points: [
                    Point(x: -118.81898, y: 34.01375, spatialReference: .wgs84),
                    Point(x: -118.80679, y: 34.02158, spatialReference: .wgs84),
                    Point(x: -118.79143, y: 34.01638, spatialReference: .wgs84),
                    Point(x: -118.79596, y: 34.00856, spatialReference: .wgs84),
                    Point(x: -118.80855, y: 34.00350, spatialReference: .wgs84)
                ]
            )
            let polygonSymbol = SimpleFillSymbol(style: .solid, color: .orange, outline: SimpleLineSymbol(style: .solid, color: .blue, width: 2.0))
    
            return graphicsOverlay
    
    Expand
  2. Create a Graphic with the polygon and polygonSymbol. Display the Graphic by adding it to the graphicsOverlay's GraphicsOverlay.graphics collection.

    Expand
    Use dark colors for code blocks
    42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59 59
    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
            graphicsOverlay.addGraphic(polylineGraphic)
    
            let polygon = Polygon(
                points: [
                    Point(x: -118.81898, y: 34.01375, spatialReference: .wgs84),
                    Point(x: -118.80679, y: 34.02158, spatialReference: .wgs84),
                    Point(x: -118.79143, y: 34.01638, spatialReference: .wgs84),
                    Point(x: -118.79596, y: 34.00856, spatialReference: .wgs84),
                    Point(x: -118.80855, y: 34.00350, spatialReference: .wgs84)
                ]
            )
            let polygonSymbol = SimpleFillSymbol(style: .solid, color: .orange, outline: SimpleLineSymbol(style: .solid, color: .blue, width: 2.0))
    
            let polygonGraphic = Graphic(geometry: polygon, symbol: polygonSymbol)
            graphicsOverlay.addGraphic(polygonGraphic)
    
            return graphicsOverlay
    
    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.