Prash's Blog

How to use Routing Service in Virtual Earth / Bing May 3, 2010

Filed under: Bing — prazjain @ 8:19 pm
Tags: , , , ,

In my previous article on using Geocoding service in Virtual Earth I gave an introduction on how to geo code the address. See here.

In this article I will show how to use that geocoded data and calculate route information using Bing. In this article we would use the Bing Maps keys to authenticate rather than the client token that we used in last article as that if for staging environment. But if you are using the staging environment then I suggest that you use the staging url for the service wsdl and use the VEStagingToken webservice for authenticate (as shown in geocoding service article).

1) Add a service in your VS solution pointing to : http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc?wsdl and name it VERouterServiceReference.

2) Create a class RouteFetcher which would hold code to get route information for the locations passed by you.

3) Brief description of what the code does : It uses the Bing Maps key for authentication. It creates a WayPoint object for every location passed by you and send it to the webservice for calculating the route. In the end returning a string representation of instructions for the multi leg journey.

PS : Does anyone know how to paste code without losing indenting ! I have a real bad time pasting my codes here.

/// <summary>
 /// Plans a route through the locations passed in argument in the order specified.
 /// </summary>
 /// <param name="locations">array of string location formatted as (latitude,longitude)</param>
 /// <returns></returns>
 public string Route(string[] locations)
 {
 try
 {
 string results = "";
 RouteRequest routeRequest = new RouteRequest();
 routeRequest.Credentials = new Credentials();
 //routeRequest.Credentials.Token = _clientToken;
 routeRequest.Credentials.ApplicationId = _key;

 Waypoint[] wayPoints = new Waypoint[locations.Length];

 int pointIndex = -1;
 foreach (string point in locations)
 {
 pointIndex++;
 wayPoints[pointIndex] = new Waypoint();
 string[] digits = point.Split(',');
 // ignore the location if the string is not correctly formatted
 if (digits.Length!=2) continue;
 wayPoints[pointIndex].Location = new Location() { Latitude = double.Parse(digits[0].Trim()), Longitude = double.Parse(digits[1].Trim()) };
 if (pointIndex == 0)
 wayPoints[pointIndex].Description = "Start";
 else if (pointIndex == locations.Length)
 wayPoints[pointIndex].Description = "End";
 else
 wayPoints[pointIndex].Description = string.Format("Stop #{0}", pointIndex);

 }

 routeRequest.Waypoints = wayPoints;

 RouteServiceClient routeServiceClient = new RouteServiceClient();
 RouteResponse routeResponse = routeServiceClient.CalculateRoute(routeRequest);

 StringBuilder directions = new StringBuilder("");
 if (routeResponse.Result.Legs.Length > 0)
 {
 int instructionIndex = 0;
 int legIndex = 0;
 foreach (var leg in routeResponse.Result.Legs)
 {
 directions.Append(string.Format("Leg #{0}\n", ++legIndex));
 foreach (var itineraryItem in leg.Itinerary)
 {
 directions.Append(string.Format("{0}. {1}\n", ++instructionIndex, itineraryItem.Text));
 }
 Regex regex = new Regex("<[a-z/:]*>",RegexOptions.IgnoreCase);
 results = regex.Replace(directions.ToString(),string.Empty);
 }
 }
 else
 results = "No routes found";

 return results;

 }
 catch (Exception ex)
 {
 Console.WriteLine(ex.Message);
 return "Exception occurred";
 }

 }
Advertisements
 

One Response to “How to use Routing Service in Virtual Earth / Bing”

  1. […] Check the article on how to use the geocoded values to calculate route from point A to point B and more. How to use Routing Service. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s