Display Driving Directions

The RouteResult object returned from a successful RouteTask solve() invocation contains all of the necessary information to both visualize routes and display driving directions.

Getting a Route

Each RouteResult object contains a collection of Route objects which house individual route information. In the case of a local RouteResult, there will always be one Route object in the RouteResult as multiple routes are not supported.

List<Route> routes = mResults.getRoutes();
Route mRoute = routes.get(0);

Adding Route Geometry to the Map

The shape for the entire route can be accessed through the getRouteGraphic() method on a Route object. Unfortunately, the graphic returned from this method does not have a default symbol, so a symbol must be added.

// Access the whole route geometry and add it as a graphic
Geometry routeGeom = mRoute.getRouteGraphic().getGeometry();
Graphic symbolGraphic = new Graphic(routeGeom, new SimpleLineSymbol(Color.BLUE,3));


From the returned Route object you can obtain routing directions as a List and add them to your application. Each RouteDirection object contains textual directions information, duration, length, and associated geometries. The following snippet shows a simple way to add directions to an application with a Spinner. The onItemSelectedListener is also set to allow panning to each direction when it is selected.

// Find the spinner in your application
Spinner spinner = (Spinner)findViewById(R.id.directionsSpinner);
// Get the list of directions from the Route object
final List<RouteDirection> directions = mRoute.getRoutingDirections();
String[] flatArray = new String[directions.size()];
// Iterate through all of the directions and create formatted Strings
for(int i = 0; i < directions.size(); i++) {
   RouteDirection direction = directions.get(i);
   flatArray[i] = String.format("%s%nTime: %.1f minutes, Length: %.1f miles",
     direction.getText(), direction.getMinutes(), direction.getLength()));
// Create an array adapter for the formatted directions Strings       
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), 
  android.R.layout.simple_spinner_item, flatArray);
// Set the OnItemSelectedListener to pan to the RouteDirection whenever
// one is selected from the spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
  public void onNothingSelected(AdapterView<?> parent) { }