Display driving directions

Whether your ArcGIS Runtime app is connected or disconnected, your users can get driving directions in the form of a route on a map, turn-by-turn instructions, or both. Driving directions can be for a route between two locations (known as stops) or they can be for a number of other route types, such as:

  • A multiple stop route—A route from one location to multiple stops. For credit usage calculation (ArcGIS Online), this route is known as Simple Directions unless you want the algorithm to re-sequence the stops in the optimal order (shortest distance or shortest time), in which case the route is known as Optimized Directions. You may know these optimized directions as the traveling salesman problem.
  • A route with time windows—A route to some stops that have specific times of the day they need to be reached by the driver.
  • A route with barriers—A route that prevents traversal of some streets due to barriers (for example, street closures) or that reduces the traversal speed of some streets.
  • Drive time—Drive time refers to calculating the area that can be reached within a specified travel time or travel distance along a street network based on travel mode.

Tip:

If you'd like a ready-to-use and regularly updated network dataset (and locator) for performing offline geocoding and routing, you can license StreetMap Premium data (in mobile map package format). For details, see Add StreetMap Premium data.

Displaying driving directions is just one of many capabilities known as network analysis capabilities. Other network analysis capabilities include origin-destination (OD) cost matrix and location-allocation, which your app can consume through a geoprocessing service via a AGSGeoprocessingTask.

To display driving directions, you typically follow these steps:

  1. Create a AGSRouteTask and AGSRouteParameters that you'll use to reference the routing service.

    //create route task from San Diego service
    self.routeTask = AGSRouteTask(url: URL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/Route")!)
    // load route task
    self.routeTask.load {[weak self] (error) in
        if let error = error {
            print(error)
            return
        }
        if (self?.routeTask.loadStatus == .loaded) {
            // get default route parameters
            self?.routeTask.defaultRouteParameters { [weak self] (params: AGSRouteParameters?, error: Error?) -> Void in
                if let error = error {
                    print(error)
                    return
                }
                guard let parameters = params else {return}
                parameters.outputSpatialReference = .webMercator()
                // returns all stops in route result
                parameters.returnStops = true
                // returns route directions in route result
                parameters.returnDirections = true
                self?.routeParameters = parameters
            }
        }
    }

  2. Create the stops. If you want users to be able to type in an address or place name, then either:
    • Use a service that includes a locator, such as http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer.
    • Set up your own address locator using ArcGIS Desktop. If you set up your own address locator, you can serve it via ArcGIS Online, ArcGIS Server, or ArcGIS Enterprise portal for connected workflows or you can create a locator package from it for disconnected workflows.

    In either case your app consumes the locator via a AGSLocatorTask, as described in Search for places.

    You can also use the user's current location as the from- or to- location.

    let stop1Loc = AGSPoint(x: -1.3018598562659847E7, y: 3863191.8817135547, spatialReference: AGSSpatialReference.webMercator())
    let stop2Loc = AGSPoint(x: -1.3036911787723785E7, y: 3839935.706521739, spatialReference: AGSSpatialReference.webMercator())
     let stop1 = AGSStop(point: stop1Loc)
    let stop2 = AGSStop(point: stop2Loc)
     // add route stops
    self.routeParameters.setStops([stop1, stop2])

  3. Solve the route. You do this using the solveRoute method of the route task, passing in the parameters containing your route stops.

    self.routeTask.solveRoute(with: self.routeParameters) { [weak self] (routeResult: AGSRouteResult?, error: Error?) -> Void in
        if let error = error {
            print(error)
        }
        else {
            guard let routeResult = routeResult else {return}
            //show the resulting route on the map ...
        }
    }

  4. Do one or both of the following to prepare for displaying the resulting route when it's created:
    • To display the resulting route lines on the map, create a AGSGraphicsOverlay and read the geometry of the route from the route result.

      //show the resulting route on the map
      //also save a reference to the route object
      //in order to access directions later
      guard let routeResult = routeResult else {return}
      //
      self?.route = routeResult.routes[0]
      let routeSymbol = AGSSimpleLineSymbol(style: .solid, color: UIColor.yellow, width: 5)
      let routeGraphic = AGSGraphic(geometry: self?.route.routeGeometry, symbol: routeSymbol, attributes: nil)
      self?.routeGraphicsOverlay.graphics.add(routeGraphic)

    • You can get turn-by-turn directions by listing each AGSDirectionManeuver from the route result. Each step of the route is represented in a AGSDirectionManeuver and the class contains time, distance, geometry, and direction text describing each maneuver.

      // get the direction text for each maneuver
      for step in self.route.directionManeuvers {
          print("Direction: " + step.directionText)
      }

How much will my routing app cost?

The cost points to consider when creating your routing app are the following:

  • You might need a paid deployment plan:
    • If you want to deploy an app that you charge users for and your app uses ArcGIS Online
    • If you want to deploy an app that uses more than 50 ArcGIS Online credits per month
  • If you want to deploy an app that uses more ArcGIS Online credits than the number of credits that came with your ArcGIS Developer Subscription, then you'll need to do one of the following:
    • Purchase credits as you use them
    • Purchase a deployment plan
    • Use named users and pass the cost of credits on to those users
  • Named users in a production environment have a cost associated with them. This cost is not necessarily your cost, as the developer. Whoever owns the organization on ArcGIS Online or whoever owns the license for ArcGIS Enterprise portal pays for the named users.
  • If your app consumes services hosted by ArcGIS Server or ArcGIS Enterprise portal, then there is a cost (not necessarily to you, the developer) for licensing ArcGIS Server or ArcGIS Enterprise portal in order to host those services.
  • If your app uses a Basic, Standard, Advanced, or Analysis license level of ArcGIS Runtime, then you must purchase that license. If your app runs offline, you may have to purchase deployment packs. For a list of capabilities and the license level they require, see Licensing capabilities in "License your app."