Find route

View on GitHubSample viewer app

Display directions for a route between two points.

Solved route List of directions

Use case

Find routes with driving directions between any number of locations. You might use the ArcGIS platform to create a custom network for routing on private roads.

How to use the sample

For simplicity, the sample comes loaded with a start and end stop. You can click on the Find Route button to display a route between these stops. Once the route is generated, tap the directions icon for turn-by-turn directions shown in a list.

How it works

  1. Create a AGSRouteTask using a URL to an online route service.
  2. Generate default AGSRouteParameters using AGSRouteTask.defaultRouteParameters(completion:).
  3. Set returnDirections on the parameters to true.
  4. Create an ASGStop object for each destination and assign the stops to the parameters using AGSRouteParameters.setStops(_:).
  5. Solve the route using AGSRouteTask.solveRoute(with:completion:) to get an AGSRouteResult.
  6. Iterate through the result's AGSRoutes. To display the route, create a graphic using the geometry from the route's routeGeometry. To display directions, get the direction maneuvers from the route's directionManeuvers property, and, for each maneuver, display the maneuver's directionText.

Relevant API

  • AGSDirectionManeuver
  • AGSRoute
  • AGSRouteParameters
  • AGSRouteResult
  • AGSRouteTask
  • AGSStop

Tags

directions, driving, navigation, network, network analysis, route, routing, shortest path, turn-by-turn

Sample Code

DirectionsViewController.swiftDirectionsViewController.swiftFindRouteViewController.swift
Use dark colors for code blocksCopy
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
// Copyright 2016 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
//
//   http://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 DirectionsViewController: UITableViewController {
    var directionManeuvers = [AGSDirectionManeuver]()

    // MARK: - UITableViewDataSource

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return directionManeuvers.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DirectionsCell", for: indexPath)

        cell.textLabel?.text = directionManeuvers[indexPath.row].directionText

        return cell
    }
}

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