RouteServiceClient.CalculateRoutesFromMajorRoads Method

 

Calculates starting points or route directions to a specified location from nearby major roads.

Syntax

public MajorRoutesResponse CalculateRoutesFromMajorRoads(MajorRoutesRequest request);
Public Function CalculateRoutesFromMajorRoads(ByVal request As MajorRoutesRequest) As MajorRoutesResponse

Parameters

request

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

Return Value

Returns a MajorRoutesResponse Class.

Remarks

If no major roads are found near the specified MajorRoutesRequest.Destination Property, then the MajorRoutesResponse.StartingPoints Property is returned empty and no routes are calculated.

Example

private void CalculateRoutesFromMajorRoadsRequest() {
    string Results = "";
    try {
        //  Set a Bing Maps key before making a request 
        string key = "Bing Maps Key";
        RouteService.MajorRoutesRequest majorRoutesRequest = new RouteService.MajorRoutesRequest();
        //  Set the credentials using a valid Bing Maps Key 
        majorRoutesRequest.Credentials = new RouteService.Credentials();
        majorRoutesRequest.Credentials.ApplicationId = key;
        //  Set the destination of the routes from major roads 
        RouteService.Waypoint endPoint = new RouteService.Waypoint();
        endPoint.Location = new RouteService.Location();
        endPoint.Location.Latitude = 47.608;
        endPoint.Location.Longitude = -122.337;
        endPoint.Description = "Destination";
        //  Set the option to return full routes with directions 
        RouteService.MajorRoutesOptions options = new RouteService.MajorRoutesOptions();
        options.ReturnRoutes = true;
        majorRoutesRequest.Destination = endPoint;
        majorRoutesRequest.Options = options;
        //  Make the route-from-major-roads request 
        RouteService.RouteServiceClient routeService = new RouteService.RouteServiceClient("BasicHttpBinding_IRouteService");
        RouteService.MajorRoutesResponse majorRoutesResponse = routeService.CalculateRoutesFromMajorRoads(majorRoutesRequest);
        RouteService.RouteLeg leg = new RouteService.RouteLeg();
        for (int j = 0; j < majorRoutesResponse.Routes.Length; j++) {
            directions = directions + "<b>Route " + (j + 1).ToString() + "</b><br>";
            for (int k = 0; k < majorRoutesResponse.Routes[j].Legs.Length; k++) {
                leg = majorRoutesResponse.Routes[j].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;
    }
}

See Also

RouteServiceClient.CalculateRoute Method