20 minutes

Requirements

Solution

Overview

You will learn: how to find the optimal route and directions for multiple stops with the ArcGIS Route service.

The ArcGIS Transportation Routing and Network Analytics Services can find routes, get driving directions, calculate drive times, and solve complicated multiple vehicle routing problems (VRP). If you would like to create an application that can find driving directions and create an optimized route, you can use a route task and the solveRouteAsync method. All you have to do is pass in the "stop" locations, and the service will return a route with directions. Once you have the results you can add the route to a map, display the turn-by-turn directions, or integrate them further into your application. To learn more about the capabilities of the directions and routing service, please visit the documentation.

In this tutorial you will learn how to use a routing service to calculate an optimal route between two or more places. Points will be created by user when clicking on the map.

Reuse the starter project

Configure OAuth2 for your app

Reuse the application you previously set up in the Access services with OAuth 2.0 tutorial. If you did not do that tutorial, then register your app to generate a client id and set a redirect URI to access the secure service:

Sign in to your ArcGIS account. If you don't already have one, sign-up for free.

Add graphics overlay to a map view

Add a new private method setupGraphicsOverlay that creates a graphics overlay and attaches it to the map view to render the graphics to the map. Each graphic must have a symbol assigned in order for it to be visible on the map.

Create Route Task

Initialize and load the route task using the routing service URL. The route task will calculate a route between two points using detailed information from service URL. The service URL usually gives detailed information about a certain area, so if a point was to fall outside that area then the route will only be completed up to that area's boundary.

Add listener for mouse interactions

Add a private method createRouteAndDisplay that adds a listener to the map view for interpreting user input when the map is clicked. Then call the graphic display methods you coded in the prior step.

Checks to see if the user clicked the primary mouse button, left mouse button, within the map view and creates a point from the map if so. Once the point is created it will be used for either the start or end point graphic depending on which one doesn't have a point location already stored. If both start and end have a point then the application will clear out the old route and start a new one.

Check that a route was returned and selected the first one. Display this route to the map using the graphics overlay.

A route result could contain more than one solution. In this tutorial you just select the first solution and ignore any others. It is also possible it returns no solution.

The route is represented with a Polyline that is provided by the route task solution. You only need to add it to your map. Graphics are displayed on the MapView with a GraphcisOverlay and conveniently you can use the same one showing the markers.

Congratulations, you're done!

Run your application and when you click two different locations within the map view a route will be displayed between those points. Compare with our completed solution project.

Challenge

There are several ways this app could be enhanced. Can you take it to the next level?

Explore route parameters

The RouteParameters class offers a lot of options. Explore some of these options and enhance your app. Can you show turn-by-turn directions?

Get additional information about the route

The solution route, called firstRoute in this example, has additional information accessible through a series of .get...() statements. Add the following lines of code to solveForRoute() after getting firstRoute: