RouteServiceClient.CalculateRoute Method

 

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

Syntax

public RouteResponse CalculateRoute(RouteRequest request)
Public Function CalculateRoute(ByVal request As RouteRequest) As RouteResponse

Parameters

request

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

Return Value

Returns a RouteResponse Class, which contains a RouteResult Class.

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;
    }
}
Private Sub CalculateRouteRequest() Dim Results As String 
    Try
'  Set a Bing Maps key before making a request 
        Dim key = "Bing Maps Key" Dim routeRequest As New RouteService.RouteRequest()
    '  Set the credentials using a valid Bing Maps key 
        routeRequest.Credentials = _ New RouteService.Credentials() With {
            .ApplicationId = key
        }

    '  Set the start, stop, and end points 
        Dim waypoints As RouteService.Waypoint() = _ { 
            _ New RouteService.Waypoint() _ With {
                .Description = "Start", _ .Location = _ New RouteService.Location() With {
                    .Latitude = 40, .Longitude = -120
                }
            }, 
            _ New RouteService.Waypoint() _ With {
                .Description = "Stop", _ .Location = _ New RouteService.Location() With {
                    .Latitude = 40.5, .Longitude = -120.5
                    }
            }, 

            _ New RouteService.Waypoint() _ With {
                .Description = "End", _ .Location = _ New RouteService.Location() With {
                    .Latitude = 41, .Longitude = -121
                }
            } _ 
        } 

        routeRequest.Waypoints = waypoints

'  Make the calculate route request 
        Dim RouteServiceInstance As New RouteService.RouteServiceClient("BasicHttpBinding_IRouteService") 
        Dim routeResponse = RouteServiceInstance.CalculateRoute(routeRequest)

'  Iterate through each itinerary item to get the route directions 
        Dim leg As RouteService.RouteLeg 
        Dim directions = "" F or k = 0 To 
        routeResponse.Result.Legs.Length - 1 
        leg = routeResponse.Result.Legs(k) 

        For i = 0 To leg.Itinerary.Length - 1 
            directions = directions & leg.Itinerary(i).Text & "<br>" 
            Next Next Results = directions 

    Catch ex As Exception Results = "An exception occurred: " & ex.Message 

    End Try 
End Sub

See Also

RouteServiceClient.CalculateRoutesFromMajorRoads Method