Find a route and directions

Learn how to find a route and directions with the route service.

find a route and directions

Routing is the process of finding the path from an origin to a destination in a street network. You can use the Routing service to find routes, get driving directions, calculate drive times, and solve complicated, multiple vehicle routing problems. To create a route, you typically define a set of stops (origin and one or more destinations) and use the service to find a route with directions. You can also use a number of additional parameters such as barriers and mode of travel to refine the results.

In this tutorial, you define an origin and destination by clicking on the map. These values are used to get a route and directions from the route service. The directions are also displayed on the map.

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 a 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
          }
      
      }
      

Update the map

A navigation basemap layer is typically used in routing applications. Update the basemap to use the .arcGISNavigation basemap style, and change the position of the map to center on Los Angeles.

  1. Update the AGSBasemap style property from .arcGISTopographic to .arcGISNavigation and update the latitude and longitude coordinates to center on Los Angeles.

    ViewController.swift
    Change lineChange lineChange lineChange lineChange lineChange lineChange lineChange line
    30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 29 28 27 26 26 26 27 28 28 28 29 30 31 32 33 34 35 36 37 37 36 35 34 33 32 31 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 2 1 0 -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 -96 -96 -96 -97 -98 -99 -100 -101 -102 -104 -106 -108 -110 -112 -114 -116 -118 -120 -122 -124 -126 -128 -130 -132 -134 -136 -138 -140 -142 -144 -145 -146 -147
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    

Receive map view touch events

The app will use locations derived from a user tapping the map view to generate the stops of a route. Conform the view controller to receive touch events from the map view.

The locations derived from a user tapping the map view will be used to generate routes in a later step.

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

  2. In the editor, extend ViewController to conform to the AGSGeoViewTouchDelegate protocol and include the geoView:didTapAtScreenPoint:mapPoint:() geoview touch delegate method.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 179 178 177 176 175 175 175 175 175 174 173 173 173 173 173 173 173 173 173 173 173 172 171 170 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 40 40 41 42 43 44 45 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 47 48 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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    
  3. In the setupMap() method, assign ViewController to mapView.delegate.

    This step bridges mapView user touch interactions with ViewController via the AGSGeoViewTouchDelegate protocol.

    ViewController.swift
    Add line.
    30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 29 28 27 26 26 26 27 28 29 30 31 32 33 34 35 36 37 38 39 39 38 37 36 35 34 33 32 31 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 2 1 0 -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 -94 -94 -94 -94 -94 -94 -94 -94 -95 -96 -97 -98 -99 -100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -115 -115 -115
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    

Add graphics to the map view

A graphics overlay is a container for graphics. Graphics are added as a visual means to display the search result on the map.

  1. Create a private AGSGraphic property named startGraphic. This graphic will be used to display the route's start location.

    An AGSSimpleMarkerSymbol is used to display a location on the map view.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.
    44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 43 42 41 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 41 42 43 44 45 46 47 48 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 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 2 1 0 -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 -57 -57 -57 -57 -57 -57 -57 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -72 -73 -74 -75 -76 -77 -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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    
  2. Create a private AGSGraphic property named endGraphic. This graphic will be used to display the route's end location.

    An AGSSimpleMarkerSymbol is used to display a location on the map view.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.
    44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 43 42 41 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 56 56 56 56 56 56 56 56 56 56 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 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 2 1 0 -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 -50 -50 -50 -50 -50 -50 -50 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -71 -71 -71
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    
  3. Create a private AGSGraphic property named routeGraphic. This graphic will be used to display the route line.

    An AGSSimpleLineSymbol is used to display a line on the map view.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.
    53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 52 51 50 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 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 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 2 1 0 -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 -50 -50 -50 -50 -50 -50 -50 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 -66 -67 -68 -69 -70 -71 -71 -71 -71
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    
  4. Define a private method named addGraphics(). In addGraphics() create an AGSGraphicsOverlay. Append startGraphic, endGraphic, and routeGraphic to the graphics overlay and add the graphics overlay to the map view.

    Because startGraphic, endGraphic, and routeGraphic haven't yet specified a geometry, they will not be visible.

    ViewController.swift
    Add line.Add line.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 59 58 57 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 57 58 59 60 61 62 63 64 65 66 67 67 66 65 64 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 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 2 1 0 -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 -38 -38 -38 -38 -38 -38 -38 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -59 -59 -59
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    
  5. In the viewDidLoad() method, call addGraphics().

    ViewController.swift
    Add line.
    20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 22 23 24 25 26 26 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 27 27 27 27 27 27 27 27 27 27 27 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 2 1 0 -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 -78 -78 -78 -78 -78 -78 -78 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 -97 -98 -99 -99 -99 -99
    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
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    // 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 ArcGIS
    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var mapView: AGSMapView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupMap()
    
            addGraphics()
    
            navigationItem.rightBarButtonItem = directionsButton
    
        }
    
        private func setupMap() {
    
            mapView.touchDelegate = self
    
            mapView.map = AGSMap(basemapStyle: .arcGISNavigation)
            mapView.setViewpoint(
                AGSViewpoint(
                    latitude: 34.05293,
                    longitude: -118.24368,
                    scale: 288_895
                )
            )
        }
    
        // MARK: - Route Graphics
    
        private let startGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .white, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let endGraphic: AGSGraphic = {
            let symbol = AGSSimpleMarkerSymbol(style: .circle, color: .black, size: 8)
            symbol.outline = AGSSimpleLineSymbol(style: .solid, color: .black, width: 1)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private let routeGraphic: AGSGraphic = {
            let symbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 3)
            let graphic = AGSGraphic(geometry: nil, symbol: symbol)
            return graphic
        }()
    
        private func addGraphics() {
            let routeGraphics = AGSGraphicsOverlay()
            mapView.graphicsOverlays.add(routeGraphics)
            routeGraphics.graphics.addObjects(from: [routeGraphic, startGraphic, endGraphic])
        }
    
        // MARK: - Route Builder
    
        private enum RouteBuilderStatus {
            case none
            case selectedStart(AGSPoint)
            case selectedStartAndEnd(AGSPoint, AGSPoint)
            case routeSolved(AGSPoint, AGSPoint, AGSRoute)
    
            func nextStatus(with point: AGSPoint) -> RouteBuilderStatus {
                switch self {
                case .none:
                    return .selectedStart(point)
                case .selectedStart(let start):
                    return .selectedStartAndEnd(start, point)
                case .selectedStartAndEnd:
                    return .selectedStart(point)
                case .routeSolved:
                    return .selectedStart(point)
                }
            }
    
        }
    
        private var status: RouteBuilderStatus = .none {
            didSet {
                switch status {
                case .none:
                    startGraphic.geometry = nil
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStart(let start):
                    startGraphic.geometry = start
                    endGraphic.geometry = nil
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .selectedStartAndEnd(let start, let end):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = nil
                    directionsButton.isEnabled = false
                case .routeSolved(let start, let end, let route):
                    startGraphic.geometry = start
                    endGraphic.geometry = end
                    routeGraphic.geometry = route.routeGeometry
                    directionsButton.isEnabled = true
                }
            }
        }
    
        // MARK: - Directions Button
    
        private lazy var directionsButton: UIBarButtonItem = {
            let button = UIBarButtonItem(title: "Show Directions", style: .plain, target: self, action: #selector(displayDirections))
            button.isEnabled = false
            return button
        }()
    
        @objc
        func displayDirections(_ sender: AnyObject) {
            guard case let .routeSolved(_, _, route) = status else { return }
            let directions = route.directionManeuvers.enumerated()
                .reduce(into: "\n") { $0 += "\($1.offset + 1). \($1.element.directionText).\n\n" }
            let alert = UIAlertController(title: "Directions", message: directions, preferredStyle: .alert)
            let okay = UIAlertAction(title: "Hide Directions", style: .default, handler: nil)
            alert.addAction(okay)
            present(alert, animated: true, completion: nil)
        }
    
        // MARK: - Route Task
    
        private let routeTask: AGSRouteTask = {
            let worldRoutingService = URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!
            return AGSRouteTask(url: worldRoutingService)
        }()
    
        private var currentSolveRouteOperation: AGSCancelable?
    
        private func solveRoute(start: AGSPoint, end: AGSPoint, completion: @escaping (Result<[AGSRoute], Error>) -> Void) {
    
            currentSolveRouteOperation?.cancel()
    
            currentSolveRouteOperation = routeTask.defaultRouteParameters { [weak self] (defaultParameters, error) in
                guard let self = self else { return }
                if let error = error {
                    completion(.failure(error))
                    return
                }
    
                guard let params = defaultParameters else { return }
                params.returnDirections = true
                params.setStops([AGSStop(point: start), AGSStop(point: end)])
    
                self.currentSolveRouteOperation = self.routeTask.solveRoute(with: params) { (routeResult, error) in
                    if let routes = routeResult?.routes {
                        completion(.success(routes))
                    } else if let error = error {
                        completion(.failure(error))
                    }
                }
    
            }
    
        }
    
    }
    
    // MARK: - GeoView Touch Delegate
    
    extension ViewController: AGSGeoViewTouchDelegate {
    
        func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
    
            currentSolveRouteOperation?.cancel()
    
            status = status.nextStatus(with: mapPoint)
    
            if case let .selectedStartAndEnd(start, end) = status {
                solveRoute(start: start, end: end) { [weak self] (result) in
                    guard let self = self else { return }
                    switch result {
                    case .failure(let error):
                        print(error.localizedDescription)
                        self.status = .none
                    case .success(let routes):
                        if let route = routes.first {
                            self.status = .routeSolved(start, end, route)
                        } else {
                            self.status = .none
                        }
                    }
                }
            }
    
        }
    
    }
    

Define app route builder status

The app will leverage a state-machine design pattern to ensure the contents of the map reflect the state of gathering the route parameters and generating the route result. This design pattern supports wrangling multiple asynchronous requests to the world routing service into a single state variable result, ensuring the reliability of route results.

  1. Define an enum named RouteBuilderStatus with four cases. These four cases are used to gather route parameters and display the route's result, over time.

    ViewController.swift
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 71 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 70 71 72 73 74 75 76 77 78 78 78 78 78 78 78 78 78 78 78 78 78 78 79 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80