windowsforms-116116-controls-and-libraries-map-control-examples-gis-data-routing-how-to-calculate-a-route-by-addresses.md
To send a route request, use the BingRouteDataProvider.CalculateRoute method. This method receives a list of RouteWaypoint objects as a parameter. Note that RouteWaypoint objects are created using the constructor that receives the waypoint description and a keyword that is used to search for a location on a map.
using DevExpress.XtraMap;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ZoomToFitOnRouteCalculated {
public partial class Form1 : Form {
const String yourBingKey = "Insert Your Key Here";
public Form1() {
InitializeComponent();
imageProvider.BingKey = yourBingKey;
}
private void Form1_Load(object sender, EventArgs e) {
// Create a new route provider.
BingRouteDataProvider provider = new BingRouteDataProvider { BingKey = yourBingKey };
informationLayer.DataProvider = provider;
// Send a request and handle request result.
provider.RouteCalculated += OnRouteCalculated;
// Note that waypoints are created using address instead of the location.
provider.CalculateRoute(new List<RouteWaypoint>() {
new RouteWaypoint("New York", "Belmont Park, New York, USA"),
new RouteWaypoint("Las Vegas", "Lorenzi Park, Las Vegas, USA")});
splashScreenManager1.ShowWaitForm();
}
void OnRouteCalculated(object sender, BingRouteCalculatedEventArgs e) {
SearchBoundingBox box = e.CalculationResult.RouteResults[0].BoundingBox;
GeoPoint topLeft = new GeoPoint {
Latitude = box.NorthLatitude,
Longitude = box.WestLongitude
};
GeoPoint bottomRight = new GeoPoint {
Latitude = box.SouthLatitude,
Longitude = box.EastLongitude
};
mapControl.ZoomToRegion(topLeft, bottomRight, 0.4);
splashScreenManager1.CloseWaitForm();
}
}
}
Imports DevExpress.XtraMap
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Namespace ZoomToFitOnRouteCalculated
Partial Public Class Form1
Inherits Form
Private Const yourBingKey As String = "Insert Your Key Here"
Public Sub New()
InitializeComponent()
imageProvider.BingKey = yourBingKey
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' Create a new route provider.
Dim provider As BingRouteDataProvider = New BingRouteDataProvider With {.BingKey = yourBingKey}
informationLayer.DataProvider = provider
' Send a request and handle request result.
AddHandler provider.RouteCalculated, AddressOf OnRouteCalculated
' Note that waypoints are created using address instead of the location.
provider.CalculateRoute(New List(Of RouteWaypoint)() From { _
New RouteWaypoint("New York", "Belmont Park, New York, USA"), _
New RouteWaypoint("Las Vegas", "Lorenzi Park, Las Vegas, USA") _
})
splashScreenManager1.ShowWaitForm()
End Sub
Private Sub OnRouteCalculated(ByVal sender As Object, ByVal e As BingRouteCalculatedEventArgs)
Dim box As SearchBoundingBox = e.CalculationResult.RouteResults(0).BoundingBox
Dim topLeft As GeoPoint = New GeoPoint With {.Latitude = box.NorthLatitude, .Longitude = box.WestLongitude}
Dim bottomRight As GeoPoint = New GeoPoint With {.Latitude = box.SouthLatitude, .Longitude = box.EastLongitude}
mapControl.ZoomToRegion(topLeft, bottomRight, 0.4)
splashScreenManager1.CloseWaitForm()
End Sub
End Class
End Namespace