Display directions for a route between two points.
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
- Create a
AGSRouteTask
using a URL to an online route service. - Generate default
AGSRouteParameters
usingAGSRouteTask.defaultRouteParameters(completion:)
. - Set
returnDirections
on the parameters totrue
. - Create an
ASGStop
object for each destination and assign the stops to the parameters usingAGSRouteParameters.setStops(_:)
. - Solve the route using
AGSRouteTask.solveRoute(with:completion:)
to get anAGSRouteResult
. - Iterate through the result's
AGSRoute
s. To display the route, create a graphic using the geometry from the route'srouteGeometry
. To display directions, get the direction maneuvers from the route'sdirectionManeuvers
property, and, for each maneuver, display the maneuver'sdirectionText
.
Relevant API
- AGSDirectionManeuver
- AGSRoute
- AGSRouteParameters
- AGSRouteResult
- AGSRouteTask
- AGSStop
Tags
directions, driving, navigation, network, network analysis, route, routing, shortest path, turn-by-turn
Sample Code
// 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
}
}