Skip To Content

Display driving directions

In this topic

To calculate routes and obtain directions, the API provides a specific class called RouteTask. RouteTask can return turn-by-turn driving directions for the route if you enable ReturnDirections on RouteParameters.

RouteTask relies on a local ArcGIS Network Dataset on your machine to compute routes, or access to a remote ArcGIS Network Analyst Service. Network Analyst services can be hosted in Esri's cloud platform, ArcGIS Online, or can be published on your own on-premises ArcGIS servers. These services provide an ArcGIS REST API for clients such as mobile and web applications. To find the URL for a Network Analyst service, you can use the ArcGIS Services Directory.

To learn more about RouteTask, see Find a route.

Geocode input addresses

When working with driving directions, users often provide input start and end addresses to be transformed to points on the map, which are then used as input stops to a RouteTask to determine the best route and display the route on the map along with turn-by-turn directions. The process of transforming an address or place name to a geographical location on a map is known as geocoding.

To learn more about geocoding, see the Add geocoding to your app tutorial. To learn how to geocode addresses and find driving directions, see the Get driving directions tutorial.

Get directions

If you enabled ReturnDirections on RouteParameters, the returned RouteResult object contains a set of turn-by-turn driving directions that you can add to your app. Each RouteDirection object contains textual directions information, duration, length, and associated geometries for one step of a route.

The following code sample shows how to enable ReturnDirections on RouteParameters:

Esri.ArcGISRuntime.Tasks.NetworkAnalyst.RouteParameters routeParams = await _routeTask.GetDefaultParametersAsync();
routeParams.ReturnDirections = true;

The following sections describe some of the direction properties you can set on the RouteParameters class. Using the RouteParameters class, you can specify the distance units to use through the DirectionsLengthUnits property, you can specify which language to use through the DirectionsLanguage property, and you can specify the direction style using the DirectionsStyleName property. Because an ArcGIS Network Analyst Service supports specific languages, linear units, and style, it's worth checking the information about the service available in the ArcGIS Services Directory, which can be accessed using the service URL. For more information about services, see Using the Services Directory.

Direction language

Depending on the languages supported by the ArcGIS Network Analyst Service, you can specify which language to use through the DirectionsLanguage property.

The following code sample shows how to specify the language, English (United States), using the DirectionsLanguage property:

routeParams.DirectionsLanguage = CultureInfo.CreateSpecificCulture( "en-US" );

Direction length units

Depending on the direction length units supported by the ArcGIS Network Analyst Service, you can specify the length information that will be reported in the direction using the DirectionsLengthUnits property on RouteParameters. The default is defined by the service used.

Possible types include the following:

  • Feet
  • Inches
  • Kilometers
  • Meters
  • Miles
  • Millimeters
  • NauticalMiles
  • Yards

The following code sample shows how to specify kilometers as the DirectionsLengthUnits property:

routeParams.DirectionsLengthUnit = Esri.ArcGISRuntime.Geometry.LinearUnits.Kilometers;

Direction style name

Depending on the direction style supported by the ArcGIS Network Analyst Service, you can specify the DirectionsStyleName property out of the following:

  • "NA Desktop"—Turn-by-turn directions suitable for printing.
  • "NA Navigation"—Turn-by-turn directions designed for an in-vehicle navigation device.
  • "NA Campus"—Turn-by-turn walking directions designed for pedestrian routes.

Display directions

The RouteResult object contains a set of RouteDirection objects representing turn-by-turn driving directions that you can add to your application. Each RouteDirection object contains textual directions information, duration, length, and associated geometries for one step of a route. You can iterate over the RouteDirection collection, and build a string for each step of the directions' textual information, duration and length. Once you have a formatted string for each step of the directions, you need to display this information in your app, and .NET provides many ways to do this. The approach shown in the following code is to create a TextBlock containing the text and add it to a StackPanel.

The following code sample shows how to iterate over the RouteDirection collection and format a string for each driving direction for display in a text block. To keep the example simple, the formatting used is very basic. This code will append a step number, followed by a TAB , followed by the directions text, and finally a new line. The step variable is then incremented.

//Define the following variables to store driving directions as well as information about the route
                var directionsBuilder = new System.Text.StringBuilder();
                var totalMiles = 0.0;
                var totalMinutes = 0.0;
                var step = 1;

//Create the following loop to iterate over each RouteDirection in the RouteDirections collection:
                foreach (var d in route.RouteDirections)
                {
//Append the current directions text to the directionsBuilder variable (StringBuilder):
                    directionsBuilder.AppendFormat("{0} \t {1}", step.ToString(), d.Text + "\n");
                    step++;
// Keep a running total of the segment length (miles) and time (minutes):
                    totalMiles += d.GetLength(LinearUnits.Miles);
                    totalMinutes += d.Time.TotalMinutes;
                }

// Display the driving information and directions in Textboxes and Textblocks
                this.RouteDistance.Text = totalMiles.ToString("0.00") + " miles";
                this.RouteTime.Text = totalMinutes.ToString("0.00") + " minutes";
               this.DirectionsTextBlock.Text = directionsBuilder.ToString() ;

The following image shows the driving directions displayed in the text block:

Map with route and driving directions displayed in the text block

Additional information

For additional information about working with driving directions, see the Get driving directions tutorial.

To learn more about using geocoding functionality with ArcGIS Runtime SDK for .NET, see the Add geocoding to your app tutorial.

Related topics