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.
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
<telerik:RadMap x:Name="myMap">
<telerik:RadMap.Provider>
<telerik:BingRestMapProvider ApplicationId="Bing_Key"
x:Name="restProvider"
CalculateRouteCompleted="BingRestMapProvider_CalculateRouteCompleted"
CalculateRouteError="BingRestMapProvider_CalculateRouteError"
Mode="Aerial"/>
</telerik:RadMap.Provider>
<telerik:VisualizationLayer Name="routeLayer"
UseBitmapCache="False"/>
<telerik:VisualizationLayer Name="wayPointsLayer"/>
</telerik: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());
}
Private wayPoints As New LocationCollection()
Private request As BingRestRouteRequest
Public Sub New()
InitializeComponent()
Me.wayPointsLayer.ItemsSource = Me.wayPoints
request = New BingRestRouteRequest()
request.Options.RouteAttributes = BingRestRouteAttributes.RoutePath
Me.restProvider.CalculateRouteAsync(request)
End Sub
Private Sub BingRestMapProvider_CalculateRouteCompleted(sender As Object, e As BingRestRoutingCompletedEventArgs)
End Sub
Private Sub BingRestMapProvider_CalculateRouteError(sender As Object, e As BingRestCalculateRouteErrorEventArgs)
MessageBox.Show(e.Error.ToString)
End Sub
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);
}
Private wayPoints As New LocationCollection()
Private request As BingRestRouteRequest
Public Sub New()
InitializeComponent()
Me.wayPointsLayer.ItemsSource = Me.wayPoints
request = New BingRestRouteRequest()
request.Options.RouteAttributes = BingRestRouteAttributes.RoutePath
wayPoints.Add(New Location(52.5629950395581, 13.359375))
wayPoints.Add(New Location(48.835797462431, 2.2412109375))
For Each point In wayPoints
request.Waypoints.Add(point.ToString)
Next
Me.restProvider.CalculateRouteAsync(request)
End Sub
Private Sub BingRestMapProvider_CalculateRouteCompleted(sender As Object, e As BingRestRoutingCompletedEventArgs)
End Sub
Private Sub BingRestMapProvider_CalculateRouteError(sender As Object, e As BingRestCalculateRouteErrorEventArgs)
MessageBox.Show(e.Error.ToString)
End Sub
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;
}
Private Sub BingRestMapProvider_CalculateRouteCompleted(sender As Object, e As BingRestRoutingCompletedEventArgs)
Dim route As Telerik.Windows.Controls.DataVisualization.Map.BingRest.Route = e.Route
If route IsNot Nothing Then
Dim routeLine As PolylineData = Me.CreateNewPolyline(Colors.Blue, 5)
' Building the polyline representing the route.'
For Each coordinatePair As Double() In route.RoutePath.Line.Coordinates
Dim point As New Location(coordinatePair(0), coordinatePair(1))
routeLine.Points.Add(point)
Next
Me.routeLayer.Items.Add(routeLine)
' Bringing the route into view.'
Dim bbox As Double() = e.Route.BoundingBox
Dim rect As New LocationRect(New Location(bbox(2), bbox(1)), New Location(bbox(0), bbox(3)))
Me.myMap.SetView(rect)
End If
End Sub
Private Function CreateNewPolyline(color As Color, thickness As Double) As PolylineData
Dim routeLine As New PolylineData
routeLine.Points = New LocationCollection()
Dim shapeFill As New MapShapeFill
shapeFill.Stroke = New SolidColorBrush(color)
shapeFill.StrokeThickness = thickness
shapeFill.StrokeDashArray = If(Me.request.Options.Mode = BingRestTravelMode.Walking, New DoubleCollection() From {2, 1}, New DoubleCollection())
routeLine.ShapeFill = shapeFill
Return routeLine
End Function