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.
  3. The ArcGIS Runtime API for iOS is installed.

Steps

Open an 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.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

    2. In Xcode, in the Project Navigator, click AppDelegate.swift.

    3. In the editor, set the apiKey property on the AGSArcGISRuntimeEnvironment with your API key.


      AppDelegate.swift
      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
      // Copyright 2021 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
      //
      //   https://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
      
      
      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
          var window: UIWindow?
      
          func application(_ application: UIApplication,
                           didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
              // Note: it is not best practice to store API keys in source code.
              // The API key is referenced here for the convenience of this tutorial.
      
              AGSArcGISRuntimeEnvironment.apiKey = "YOUR_API_KEY"
      
              return true
          }
      
      }
      

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
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        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.

    Point graphics support a number of symbol types such as AGSSimpleMarkerSymbol, PictureMarkerSymbol and AGSTextSymbol. Learn more about symbols in the API documentation.

    Add line.Add line.Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    

    You can also create AGSPoints 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):

     
    1
    AGSPointMakeWGS84(34.0005930608889, -118.80657463861)
  2. Create an AGSGraphic with the point and pointSymbol. Display the AGSGraphic by adding it to the graphicsOverlay's graphics collection.

    Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    
  3. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, 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 graphic

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

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

    Line graphics support a number of symbol types such as AGSSimpleLineSymbol and AGSTextSymbol. Learn more about symbols in the API documentation.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    
  2. Create an AGSGraphic with the polyline and polylineSymbol. Display the AGSGraphic by adding it to the graphicsOverlay's graphics collection.

    Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    
  3. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, 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 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.

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.

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

    Polygon graphics support a number of symbol types such as AGSSimpleFillSymbol, AGSPictureFillSymbol, AGSSimpleMarkerSymbol and AGSTextSymbol. Learn more about symbols in the API documentation.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    
  2. Create an AGSGraphic with the polygon and polygonSymbol. Display the AGSGraphic by adding it to the graphicsOverlay's graphics collection.

    Add line.Add line.Add line.
    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
    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
    // Copyright 2020 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
    //
    // https://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
    
    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)
    
    
            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)
    
            let pointGraphic = AGSGraphic(geometry: point, symbol: pointSymbol)
    
            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)
    
            let polylineGraphic = AGSGraphic(geometry: polyline, symbol: polylineSymbol)
    
            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))
    
            let polygonGraphic = AGSGraphic(geometry: polygon, symbol: polygonSymbol)
    
            graphicsOverlay.graphics.add(polygonGraphic)
    
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupMap()
    
            addGraphics()
    
        }
    
    }
    
  3. Press <Command+R> to run the app.

    If you are using the Xcode simulator your system must meet these minimum requirements: macOS Catalina, Xcode 11, 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 platform 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.