Routing in Virtual Earth Web Service

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.


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.


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();
            TableCell tCell0 = new TableCell();
            tCell0.VerticalAlign = VerticalAlign.Top;
            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;


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

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=”” >
<head runat=”server”>
    <title>Untitled Page</title>
    <form id=”form1″ runat=”server”>
        <br />
            <asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click”
                Text=”Get Route” />
        <asp:Label ID=”Label3″ runat=”server” Text=”Results”></asp:Label>
        <br />
        <asp:Table ID=”Table1″ runat=”server” Height=”387px” Width=”444px”>

Here’s what you should end up with:


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.


Share on twitter Share on facebook Share on linkedin Share on linkedin