Skip to main content
All docs
V25.1
  • AzureRouteDataProvider Class

    Allows you to use the Azure Maps Route service to build a route.

    Namespace: DevExpress.Xpf.Map

    Assembly: DevExpress.Xpf.Map.v25.1.dll

    NuGet Package: DevExpress.Wpf.Map

    Declaration

    public class AzureRouteDataProvider :
        AzureMapDataProviderBase

    Remarks

    The AzureRouteDataProvider allows you to connect to the Azure Maps Route service. Assign this object to the InformationLayer.DataProvider property to enable the service and allow users to calculate a route on a map.

    Azure Maps services support JSON response formats. Install the System.Text.Json package in projects that target .NET Framework to parse the Azure server response and display information on a map.

    Calculate a Route Between Locations

    Call the AzureRouteDataProvider.CalculateRoute method to calculate a route between the specified origin and destination, passing through the selected waypoints.

    The AzureRouteOptions parameter of the AzureRouteDataProvider.CalculateRoute method allows you to specify route options.

    The following settings are available:

    AzureRouteOptions.TravelMode
    Specifies the transportation / commute mode.
    AzureRouteOptions.MaxAlternatives
    Specifies the number of alternative routes to be calculated. Default: 0, minimum: 0 and maximum: 5.
    AzureRouteOptions.AvoidTypes
    Specifies items that AzureRouteDataProvider should try to avoid when calculating the route.
    AzureRouteOptions.CustomParameters
    Specifies additional route definitions.

    The following example calculates a car-optimized route through the specified waypoints:

    DevExpress MapControl for WPF - A route calculated through three waypoints

    <Window xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"...>
        <Window.Resources>
            <sys:String x:Key="azureKey">
                Your Azure Maps key string here.
            </sys:String>
        </Window.Resources>
        <Grid>
            <dxm:MapControl Loaded="MapControl_Loaded" x:Name="mapControl">
                <dxm:ImageLayer>
                    <dxm:AzureMapDataProvider AzureKey="{StaticResource azureKey}" Tileset="BaseRoad"/>
                </dxm:ImageLayer>
                <dxm:InformationLayer x:Name="infoLayer">
                    <dxm:AzureRouteDataProvider x:Name="routeProvider" AzureKey="{StaticResource azureKey}"/>
                </dxm:InformationLayer>
            </dxm:MapControl>
        </Grid>
    </Window>
    
    using DevExpress.Xpf.Map;
    using System.Windows;
    
    namespace WpfMapExample {
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
            private void MapControl_Loaded(object sender, RoutedEventArgs e) {
    
                List<RouteWaypoint> waypoints = new List<RouteWaypoint> {
                       new RouteWaypoint("NY", new GeoPoint(41.145556, -73.995)),
                       new RouteWaypoint("Oklahoma", new GeoPoint(36.131389, -95.937222)),
                       new RouteWaypoint("Las Vegas", new GeoPoint(36.175, -115.136389))
                };
                // Calculate a route between three waypoints:
                routeProvider.CalculateRoute(waypoints, new AzureRouteOptions() {
                    TravelMode = AzureTravelMode.Car,
                    AvoidTypes = AzureRouteAvoidType.AlreadyUsedRoads
                });
            }
            private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs args) {
                mapControl.ZoomToFit(args.Items);
            }
        }
    }
    

    Obtain Routing Result

    The following example calculates a route and displays route details such the estimated duration and distance:

    DevExpress Map Control for WPF - Get route info example

    The Calculate route button click initiates the routing request. The CalculateRoute method is called in the button click event handler.

    The RouteCalculated event is handled to process the request result. e.CalculationResult gets the AzureRouteCalculationResult object that contains the result of the route calculation and allow you to obtain the following information:

    <Window xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
            xmlns:dxm="http://schemas.devexpress.com/winfx/2008/xaml/map"
            xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" ...>
        <Window.Resources>
            <sys:String x:Key="azureKey">Your AzureMaps key here</sys:String>
        </Window.Resources>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <dxe:ListBoxEdit Grid.Column="0" Grid.Row="1" x:Name="listbox"/>
            <dx:SimpleButton Grid.Row="0" Content="Calculate route" Click="SimpleButton_Click"/>
            <dxm:MapControl Loaded="MapControl_Loaded" x:Name="mapControl" Grid.Column="1" Grid.RowSpan="2"
                            ToolTipEnabled="True" 
                            ShowSearchPanel="False" 
                            ZoomLevel="3">
                <dxm:MapControl.ZoomTrackbarOptions>
                    <dxm:ZoomTrackbarOptions Visible="False" />
                </dxm:MapControl.ZoomTrackbarOptions>
                <dxm:ImageLayer>
                    <dxm:AzureMapDataProvider AzureKey="{StaticResource azureKey}" Tileset="BaseRoad"/>
                </dxm:ImageLayer>
                <dxm:InformationLayer x:Name="infoLayer">
                    <dxm:AzureRouteDataProvider x:Name="routeProvider" 
                                                AzureKey="{StaticResource azureKey}" 
                                                RouteCalculated="routeProvider_RouteCalculated"
                                                LayerItemsGenerating="OnLayerItemsGenerating" 
                                                RouteStroke="Blue"/>
                </dxm:InformationLayer>
            </dxm:MapControl>
        </Grid>
    </Window>
    
    using DevExpress.Xpf.Map;
    using System.Text;
    using System.Windows;
    
    namespace WpfMapExample {
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
            private void OnLayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs args) {
                mapControl.ZoomToFit(args.Items);
            }
            private void SimpleButton_Click(object sender, RoutedEventArgs e) {
                List<RouteWaypoint> waypoints = new List<RouteWaypoint> {
                       new RouteWaypoint("NY", new GeoPoint(41.145556, -73.995)),
                       new RouteWaypoint("Oklahoma", new GeoPoint(36.131389, -95.937222)),
                       new RouteWaypoint("Las Vegas", new GeoPoint(36.175, -115.136389))
                };
                // Calculate a route between three waypoints:
                routeProvider.CalculateRoute(waypoints, new AzureRouteOptions() {
                    TravelMode = AzureTravelMode.Car,
                    AvoidTypes = AzureRouteAvoidType.AlreadyUsedRoads
                });
            }
            private void routeProvider_RouteCalculated(object sender, AzureRouteCalculatedEventArgs e) {
                AzureRouteCalculationResult result = e.CalculationResult;
                if ((result.RouteResults == null) ||
                    (result.ResultCode == RequestResultCode.BadRequest)) {
                    listbox.Items.Add("The Azure Route service does not work for this location.");
                    return;
                }
                StringBuilder resultList = new StringBuilder("");
                if (result.IntermediatePoints != null) {
                    resultList.Append(String.Format("_________________________\n"));
                    for (int i = 0; i < e.CalculationResult.IntermediatePoints.Count; i++)
                        resultList.Append(
                            String.Format("Waypoint {0}: {1} ({2})\n",
                            i + 1,
                            e.CalculationResult.IntermediatePoints[i].Description,
                            e.CalculationResult.IntermediatePoints[i].Location)
                        );
                }
                if (result.RouteResults != null) {
                    for (int rnum = 0; rnum < e.CalculationResult.RouteResults.Count; rnum++) {
                        var routeSummary = e.CalculationResult.RouteResults[rnum].Summary;
                        resultList.AppendLine("_________________________");
                        resultList.AppendLine($"Path {rnum + 1} summary:");
                        resultList.AppendLine($"Travel Distance: {GetDistanceString(routeSummary)}");
                        resultList.AppendLine($"Travel Time: {GetTravelTimeString(routeSummary)}");
                        if (e.CalculationResult.RouteResults[rnum].Legs != null) {
                            int legNum = 1;
                            foreach (AzureRouteLeg leg in e.CalculationResult.RouteResults[rnum].Legs) {
                                resultList.AppendLine($"\tLeg {legNum++}");
                                resultList.AppendLine($"\tDistance: {GetDistanceString(leg.Summary)}");
                                resultList.AppendLine($"\tTravel Time: {GetTravelTimeString(leg.Summary)}");
                                resultList.AppendLine($"\tDeparture Time: {leg.Summary.Departure}");
                                resultList.AppendLine($"\tArrival Time: {leg.Summary.Arrival}");
                                resultList.AppendLine($"\tDeviation Time: {leg.Summary.DeviationTime}");
                            }
                        }
                    }
                }
                listbox.Items.Add(resultList.ToString());
            }
            static string GetTravelTimeString(AzureRouteSummary summary) {
                var timeSpan = TimeSpan.FromSeconds(summary.TravelTimeSeconds);
                return $"{timeSpan.Days}d, {timeSpan.Hours}hr, {timeSpan.Minutes}min";
            }
            static string GetDistanceString(AzureRouteSummary summary) {
                return String.Format("{0:0.00}km", (double)summary.TravelLengthMeters / 1000);
            }
        }
    }
    

    Implements

    See Also