New to Telerik UI for WinUI? Download free 30-day trial

Routing

RadMap provides a unified route search architecture which uses the functionality of the different routing services. This allows you to calculate a route between different locations on the map. You can access the routing service via BingRestMapProvider provider.

The BingRestMapProvider's built-in routing service wraps the Routes API of the Bing Maps Rest services.

WinUI RadMap Rad Rest Map Routing Example

Set up routing request and events

In order to get the routing data you can create instance of the BingRestRouteRequest object. Then you can call the CalculateRouteAsync() method of the BingRestMapProvider which uses the request object as a parameter. Then you can subscribe to the BingRestMapProvider's CalculateRouteCompleted and CalculateRouteError event. You can take a look at the following steps:

  • Set the ApplicationId property of the BingRestMapProvider. This property represents a key, which allows you to use the Bing Rest Maps services.

    Without supplying a valid key you won't be able to visualize the map inside the RadMap control. In order to learn how to obtain one, please read Accessing the Control Using a Bing Maps.

  • Create instance of the BingRestRouteRequest object

  • Handle CalculateRouteCompleted and CalculateRouteError events of the BingRestMapProvider
  • Call CalculateRouteAsync() method of the BingRestRouteRequest and provide the request as a parameter

The BingRestRouteRequest object provides Options property of type BingRestRouteOptions which allow the user to customize it. The following list represents this options:

  • Mode: A property of type BingRestTravelMode that gets or sets the type of direction. This mode is enumeration and it allows the following values.
    • Driving: The return route contains driving directions
    • Walking: The return route contains walking directions
  • Optimization: A property of type BingRestRouteOptimization that gets or sets the calculation method that will use. This optimization is enumeration and it allows the following values.
    • Distance: The route is calculated to minimize the total distance. Traffic information is not used.
    • Time: The route is calculated to minimize the total time. Traffic information is not used.
    • TimeWithTraffic: The route is calculated to minimize the time and uses current traffic information.
    • TimeAvoidClosure: The route is calculated to minimize the time and avoid road closures. Traffic information is not used.
  • RouteAvoidance: A property of type BingRestRouteAvoidance that gets or sets the calculation method that will use. These route attributes are enumerations and it allows the following values.
    • None: Use all roads.
    • Highways: Avoids the use of highways in the route.
    • Tolls: Avoids the use of toll roads in the route.
    • MinimizeHighways: Minimizes (tries to avoid) the use of highways in the route.
    • MinimizeTolls: Minimizes (tries to avoid) the use of toll roads in the route.
  • RouteAttributes: A property of type BingRestRouteAttributes that gets or sets whether to include or exclude parts of the routes response. The default value is BingRestRouteAttributes.ExcludeItinerary. This optimization is enumeration and it allows the following values.
    • ExcludeItinerary: Use all roads.
    • RoutePath: Avoids the use of highways in the route.
    • TransitStops: Avoids the use of toll roads in the route.
    • All: Minimizes (tries to avoid) the use of highways in the route.
    • RouteSummariesOnly: Minimizes (tries to avoid) the use of toll roads in the route.

Example 1: Defining BingRestMapProvider XAML

<dataVisualization:RadMap x:Name="myMap" Margin="50"> 
    <dataVisualization:RadMap.Provider> 
        <telerikMap:BingRestMapProvider ApplicationId="Bing_Map_Key"  
                                    x:Name="restProvider" 
                                    CalculateRouteCompleted="BingRestMapProvider_CalculateRouteCompleted" 
                                    CalculateRouteError="BingRestMapProvider_CalculateRouteError"  
                                    Mode="Aerial"/> 
    </dataVisualization:RadMap.Provider> 
    <telerikMap:VisualizationLayer Name="routeLayer" 
                            UseBitmapCache="False"/> 
    <telerikMap:VisualizationLayer Name="wayPointsLayer"/> 
</dataVisualization:RadMap> 

Example 2: Defining BingRestRouteRequest object

    private LocationCollection wayPoints = new LocationCollection(); 
    public BingRestRouteRequest request { get; set; } 
    public MainWindow() 
    { 
        InitializeComponent(); 
        this.wayPointsLayer.ItemsSource = this.wayPoints; 
        request = new BingRestRouteRequest();   
        this.restProvider.CalculateRouteAsync(request); 
    }        
    private void BingRestMapProvider_CalculateRouteCompleted(object sender, BingRestRoutingCompletedEventArgs e) 
    {      
    }        
    private void BingRestMapProvider_CalculateRouteError(object sender, BingRestCalculateRouteErrorEventArgs e) 
    { 
        MessageBox.Show(e.Error.ToString()); 
    } 

Executing Routing

In order to execute a routing action you have to call the CalculateRouteAsync() method of the BingRestMapProvider and pass a BingRestRouteRequest object as its argument. The BingRestRouteRequest should contain the points which should take part in the desired order. To pass the location points to the request argument just use its Waypoints property.

Example 4: Calling the CalculateRouteAsync() methods

 private LocationCollection wayPoints = new LocationCollection(); 
    public BingRestRouteRequest request { get; set; } 
    public MainWindow() 
    { 
        InitializeComponent(); 
        this.wayPointsLayer.ItemsSource = this.wayPoints; 
        request = new BingRestRouteRequest(); 
        wayPoints.Add(new Location(52.5629950395581, 13.359375)); 
        wayPoints.Add(new Location(48.835797462431, 2.2412109375));             
        request.Options.RouteAttributes = BingRestRouteAttributes.RoutePath; 
        foreach (var point in wayPoints) 
        { 
            request.Waypoints.Add(point.ToString()); 
        } 
        this.restProvider.CalculateRouteAsync(request); 
    } 

Routing Result

The result from the routing services is held by the Route object inside the BingRestRoutingCompletedEventArgs of the handler for the CalculateRouteCompleted event.

To get the result you have to access the Points collection of the Telerik.Windows.Controls.DataVisualization.Map.BingRest.RoutePath object. In Example 5 the result is displayed by a PolylineData via the RadMap's visualization layer.

Example 5: Handling the CalculateRouteCompleted event

private void BingRestMapProvider_CalculateRouteCompleted(object sender, BingRestRoutingCompletedEventArgs e) 
{           
    Telerik.Windows.Controls.DataVisualization.Map.BingRest.Route route = e.Route; 
    if (route != null) 
    { 
        PolylineData routeLine = this.CreateNewPolyline(Colors.Blue, 5); 
 
        // Building the polyline representing the route. 
        foreach (double[] coordinatePair in route.RoutePath.Line.Coordinates) 
        { 
            Location point = new Location(coordinatePair[0], coordinatePair[1]); 
            routeLine.Points.Add(point); 
        } 
        this.routeLayer.Items.Add(routeLine); 
        // Bringing the route into view. 
        double[] bbox = e.Route.BoundingBox; 
        LocationRect rect = new LocationRect(new Location(bbox[2], bbox[1]), new Location(bbox[0], bbox[3])); 
        this.myMap.SetView(rect); 
    } 
} 
private PolylineData CreateNewPolyline(Color color, double thickness) 
{ 
    PolylineData routeLine = new PolylineData() 
    { 
        ShapeFill = new MapShapeFill() 
        { 
            Stroke = new SolidColorBrush(color), 
            StrokeThickness = thickness, 
            StrokeDashArray = this.request.Options.Mode == BingRestTravelMode.Walking ? new DoubleCollection() { 2, 1 } : new DoubleCollection(), 
        }, 
        Points = new LocationCollection(), 
    }; 
    return routeLine; 
}    

See Also

In this article
Not finding the help you need?