AzureRouteDataProvider.RouteCalculated Event
Occurs when route calculation is complete.
Namespace: DevExpress.XtraMap
Assembly: DevExpress.XtraMap.v24.2.dll
NuGet Package: DevExpress.Win.Map
#Declaration
public event AzureRouteCalculatedEventHandler RouteCalculated
#Event Data
The RouteCalculated event's data class is AzureRouteCalculatedEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Calculation |
Returns the result of a route calculation. |
Cancelled |
Gets a value indicating whether an asynchronous operation has been canceled.
Inherited from Async |
Error |
Gets a value indicating which error occurred during an asynchronous operation.
Inherited from Async |
User |
Gets the unique identifier for the asynchronous task.
Inherited from Async |
The event data class exposes the following methods:
Method | Description |
---|---|
Raise |
Raises a user-supplied exception if an asynchronous operation failed.
Inherited from Async |
#Remarks
The following example displays detailed information about the calculated route:
The 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:
An AzureRouteCalculationResult.IntermediatePoints collection. You can obtain the Description and Location of each
RouteWaypoint
in the list.An AzureRouteCalculationResult.RouteResults collection. You can obtain summary information about the route, its legs and sections. For example, TravelLengthMeters, TravelTimeSeconds Arrival and Departure time.
using DevExpress.XtraMap;
using System.Text;
// ...
const string key = "your key";
// ...
routeProvider = new AzureRouteDataProvider { AzureKey = key };
routeProvider.RouteCalculated += OnRouteCalculated;
// ...
private void OnCalculateRoutesClick(object sender, EventArgs e) {
List<RouteWaypoint> waypoints = new List<RouteWaypoint>();
waypoints.Add(new RouteWaypoint("NY", new GeoPoint(41.145556, -73.995)));
waypoints.Add(new RouteWaypoint("Oklahoma", new GeoPoint(36.131389, -95.937222)));
waypoints.Add(new RouteWaypoint("Las Vegas", new GeoPoint(36.175, -115.136389)));
routeProvider.CalculateRoute(waypoints, new AzureRouteOptions() {
TravelMode = AzureTravelMode.Car
});
}
private void OnRouteCalculated(object sender, AzureRouteCalculatedEventArgs e) {
AzureRouteCalculationResult result = e.CalculationResult;
if ((result.RouteResults == null) ||
(result.ResultCode == RequestResultCode.BadRequest)) {
MessageBox.Show("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}");
}
}
}
}
MessageBox.Show(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);
}