I’ve been working on this one for a while and it kind of completes the basics for getting started with the Virtual Earth Web Service. The Routing Service in VEWS allows for route calculation and itinerary creation.  The following will get you started using the routing service. In this sample I’ll instantiate the service request, pass the points into an array, set options for the route object, get the route object back and parse the information into a table for viewing.

First things first, add a web reference to the route service.

image

Next, the following code will provide you with the route itinerary starting at lat/lon (40, –120) then stopping for some Starbucks coffee at lat/lon (40.5, –120.5) and ending at lat/lon (41, –121). The inline documentation should help explain what’s happening. The ASPX and Code-behind are available below.

//////////////////////
//Route.ASPX.CS
//////////////////////

using System;
using System.Web;
using System.Web.UI.WebControls;
using VEWSX.TokenService;
using VEWSX.RouteService;

namespace VEWSX
{
    public partial class Route : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Labels are hidden when the page loads
            Label3.Visible = false;
            Table1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Authenticate and get a token
            string myToken = Authenticate.Authentication(Page.Request.UserHostAddress);
            //Instantiate Route Request
            RouteRequest myRouteRequest = new RouteRequest();
            myRouteRequest.Credentials = new RouteService.Credentials();
            myRouteRequest.Credentials.Token = myToken;
            //Create an array of points (this could also be addresses, versus lat/lons
            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 = "Starbucks";
            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;
            //Pass the array into the Route Request object
            myRouteRequest.Waypoints = waypoints;
            //Creation Route options
            RouteOptions myRouteOptions = new RouteOptions();
            //Set travel mode - Driving or Walking
            myRouteOptions.Mode = TravelMode.Driving;
            //Set the optimization type - MinimizeDistance or MinimizeTime
            myRouteOptions.Optimization = RouteOptimization.MinimizeDistance;
            //Set the use of traffic conditions - TrafficBasedRouteAndTime, TrafficBasedTime, or None
            myRouteOptions.TrafficUsage = TrafficUsage.None;
            //Pass the Route Options to the Route Object
            myRouteRequest.Options = myRouteOptions;
            //Instatiate Route Service Request
            RouteService.RouteServiceClient myRouteServiceClient = new RouteService.RouteServiceClient();
            //Instantiate Route response
            RouteService.RouteResponse myRouteResponse;
            //Calculate the Route
            myRouteResponse = myRouteServiceClient.CalculateRoute(myRouteRequest);
            //Instantiate Route Leg
            RouteService.RouteLeg myRouteLeg;
            //Create directions, parse them and put them in a table
            string directions = "";
            TableRow tRow = new TableRow();
            Table1.Rows.Add(tRow);
            TableCell tCell0 = new TableCell();
            tCell0.VerticalAlign = VerticalAlign.Top;
            tRow.Cells.Add(tCell0);
            tCell0.Text = "";
            for (int i = 0; i < myRouteResponse.Result.Legs.Length; i++)
            {
                myRouteLeg = myRouteResponse.Result.Legs[i];

                for (int j = 0; j < myRouteLeg.Itinerary.Length; j++)
                {
                    directions = directions + myRouteLeg.Itinerary[j].Text
                        + " (" + Math.Round(myRouteLeg.Itinerary[j].Summary.Distance, 2) + " miles, "
                        + (myRouteLeg.Itinerary[j].Summary.TimeInSeconds / 60) + "mins)"
                        + "<br/>";
                }
            }

            tCell0.Text += directions;
            Label3.Visible = true;
            Table1.Visible = true;
        }
    }
}

//////////////////////
//Route.ASPX
//////////////////////

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Route.aspx.cs" Inherits="VEWSX.Route" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <p>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                Text="Get Route" />
        </p>
        <asp:Label ID="Label3" runat="server" Text="Results"></asp:Label>
        <br />
        <asp:Table ID="Table1" runat="server" Height="387px" Width="444px">
        </asp:Table>
    </form>
</body>
</html>

Here’s what you should end up with:

image

Pretty simple, but hopefully this will provide you with the information you need to get your routing application up and running. I didn’t do error handling, so make sure you include that with your application. Specifically, when you’re making your service calls.

CP