RouteServiceClient.CalculateRoute Method

 

Calculates a route between specified stops and returns route directions as well as other route data.

public RouteResponse CalculateRoute(RouteRequest request)

request

A RouteRequest object that contains the header and parameter information for the service operation.

Example

private void CalculateRouteRequest() {
	string Results = "";
	try {
		//  Set a Bing Maps key before making a request 
		string key = "Bing Maps Key";
		RouteService.RouteRequest routeRequest = new RouteService.RouteRequest();
		//  Set the credentials using a valid Bing Maps key 
		routeRequest.Credentials = new RouteService.Credentials();
		routeRequest.Credentials.ApplicationId = key;
		//  Set the start, stop, and end points 
		RouteService.Waypoint[] waypoints = new RouteService.Waypoint[3];
		waypoints[0] = new RouteService.Waypoint();
		waypoints[0].Description = "Start";
		waypoints[0].Location = new RouteService.Location();
		waypoints[0].Location.Latitude = 40;
		waypoints[0].Location.Longitude = -120;
		waypoints[1] = new RouteService.Waypoint();
		waypoints[1].Description = "Stop";
		waypoints[1].Location = new RouteService.Location();
		waypoints[1].Location.Latitude = 40.5;
		waypoints[1].Location.Longitude = -120.5;
		waypoints[2] = new RouteService.Waypoint();
		waypoints[2].Description = "End";
		waypoints[2].Location = new RouteService.Location();
		waypoints[2].Location.Latitude = 41;
		waypoints[2].Location.Longitude = -121;
		routeRequest.Waypoints = waypoints;
		//  Make the calculate route request 
		RouteService.RouteServiceClient routeService = new RouteService.RouteServiceClient("BasicHttpBinding_IRouteService");
		RouteService.RouteResponse routeResponse = routeService.CalculateRoute(routeRequest);
		//  Iterate through each itinerary item to get the route directions 
		RouteService.RouteLeg leg;
		string directions = "";
		for (int k = 0; k < routeResponse.Result.Legs.Length; k++) {
			leg = routeResponse.Result.Legs[k];
			for (int i = 0; i < leg.Itinerary.Length; i++) {
				directions = directions + leg.Itinerary[i].Text + "<br>";
			}
		}
		Results = directions;
	} catch (Exception ex) {
		Results = "An exception occurred: " + ex.Message;
	}
}
Show: