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

Truck Routing

RadMap provides an infrastructure that allows you to use the Bing Maps Truck Routing API to get a set of locations that can be plotted on the map.

The truck routing API is accessed via the BingRestMapProvider provider, and it is used to calculate a route between different locations on the map.

Creating a Truck Routing Request

To create a request that returns the set of truck routing locations, you can define a BingRestTruckRouteRequest, set it up and pass it to the CalculateTruckRouteAsync method of BingRestMapProvider.

The request needs two settings - the route points, defined via the RoutePoints properties, and the RoutePath attribute, set via the RouteAttributes property.

The BingRestTruckRouteRequest options are described in the Truck Routing Settings section of this article.

Example 1: Defining RadMap

<dataVisualization:RadMap x:Name="radMap"> 
    <dataVisualization:RadMap.Provider> 
        <telerikMap:BingRestMapProvider x:Name="bingRestMapProvider"  
                                    ApplicationId="Bind_Map_Key"                                                                                                             
                                    CalculateTruckRouteCompleted="BingRestMapProvider_CalculateTruckRouteCompleted" 
                                    CalculateTruckRouteError="BingRestMapProvider_CalculateTruckRouteError"/> 
    </dataVisualization:RadMap.Provider> 
    <telerikMap:VisualizationLayer Name="routeLayer" /> 
</dataVisualization:RadMap>  

Example 2: Creating a truck route request

private void RequestTruckRoute() 
{ 
    BingRestTruckRouteRequest request = new BingRestTruckRouteRequest();         
    request.RoutePoints.Add(new BingRestTruckWaypoint("590 Crane Ave, Pittsburgh, PA")); 
    request.RoutePoints.Add(new BingRestTruckWaypoint("600 Forbes Ave, Pittsburgh, PA")); 
    request.Options.RouteAttributes = BingRestTruckRouteAttribute.RoutePath;             
    request.Options.VehicleSpec = new BingRestVehicleSpec() { VehicleHazardousMaterials = HazardousMaterial.Flammable }; 
 
    this.bingRestMapProvider.CalculateTruckRouteAsync(request); 
} 
The response from the BingRestTruckRouteRequest is get via the CalculateTruckRouteCompleted event of BingRestMapProvider.

Example 3: Collecting the truck route response

private void BingRestMapProvider_CalculateTruckRouteCompleted(object sender, BingRestRoutingCompletedEventArgs e) 
{ 
    var route = e.Route; 
    if (route != null) 
    { 
        PolylineData routeLine = new PolylineData() 
        { 
            ShapeFill = new MapShapeFill() { Stroke = new SolidColorBrush(Windows.UI.Colors.Green), StrokeThickness = 5, }, 
            Points = new LocationCollection(), 
        }; 
 
        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); 
 
        double[] bbox = e.Route.BoundingBox; 
        LocationRect rect = new LocationRect(new Location(bbox[2], bbox[1]), new Location(bbox[0], bbox[3])); 
        this.radMap.SetView(rect); 
    } 
} 
To listen for errors returned by the Bing Maps service, use the CalculateTruckRouteError event of BingRestMapProvider.

Example 4: Collecting truck route response errors

private void BingRestMapProvider_CalculateTruckRouteError(object sender, BingRestCalculateRouteErrorEventArgs e) 
{ 
    var error = e.Error; 
} 

Figure 1: Route shape representing the route response returned by the truck route API

WinUI RadMap Route shape representing the route response returned by the truck route API

Truck Routing Settings

BingRestTruckRouteRequest provides several different options that can be used to adjust the request. Those options are based on the parameters described by the Bing Maps Truck Routing API documentation.

The settings can be used via the Options property of BingRestTruckRouteRequest. The property is of type BingRestTruckRouteOptions and it provides the following settings:

  • Avoid: Specifies road types to minimize during the route calculation.

  • BorderRestrictions: Specifies the border crossing preference with respect to countries, states, special areas, etc.

  • DistanceBeforeFirstTurn: Specifies the distance before the first turn is allowed in the route. This parameter is useful to make sure that the vehicle has enough distance to make the first turn.

  • Heading: Specifies the initial heading for the route. The property expects values between 0 and 359 representing degress from north (0) rotation clockwise. Setting the heading of 270 degrees creates a route that initially heads west.

  • Optimize: Specifies what parameters to use for optimizing the route.

  • OptimizeWaypoints: Specifies whether the route waypoints should be rearranged in order to reduce the route cost defined with the Optimize property.

  • RouteAttributes: Specifies what parts of the route response to be included in the received result.

  • DateTime: Specifies whether predictive traffic data is used to calculate the best route for the set date time of departure. This is applicable when the Optimize property is set to TimeWithTraffic.

  • Tolerance: Specifies a set of numeric values, where each value specifies a tolerance that is used to reduce the number of points needed to display a route on a map and still maintain the route shape.

  • VehicleSpec: Describes the vehicle. This property allows you to provide information like, the vehicle's size, weight, transported hazardous materials, etc.

An additional setting is the distance unit that can be set via the DistanceUnit property of BingRestTruckRouteRequest.

Example 5: Set request options

private void RequestTruckRoute() 
{ 
    BingRestTruckRouteRequest request = new BingRestTruckRouteRequest();         
 
    request.DistanceUnit = DistanceUnit.Kilometer;   
    request.Options.RouteAttributes = BingRestTruckRouteAttribute.RoutePath;             
    request.Options.Heading = 270; 
    request.Options.Avoid = BingRestTruckRouteAvoidance.Tolls; 
    request.Options.VehicleSpec = new BingRestVehicleSpec() { VehicleHazardousMaterials = HazardousMaterial.Flammable }; 
 
    // add RoutePoints and request the route here 
} 

See Also

In this article
Not finding the help you need?