Calculating Routes in .NET: 5 minutes tutorial

This blog has moved, you should read this at my new place!

Calculating the ‘road distance’ between two points can come in handy when you want to calculate the transport costs between your company and your customers.

Google Maps is great for calculating a single route, but when you want things automated, you need some kind of web service for this, which Google doesn’t provide.

A lesser known competitor of Google Maps is Microsoft’s Bing Maps, which has a great SDK that you can get up and running in about 5 minutes, and 4 easy steps.

1. Create a Bing AppID

To be able to use the Bing Web Services, Microsoft requires you to request a unique key through this website ( you will need a Live ID ).

2. Add a Service Reference

Add the following service reference to your project:

3. Setting up your request

Next thing we do is set up the RouteRequest.

In this example I provide the latitude and longitude myself ( from a random geocoding site ), but you could use Microsoft Bing’s or Google’s geocoding service as well, to get the lat/long from any address you like.

I’ve added some commentary in the code, so it should be self-explanatory.

// Get a key at
string key = "xxx";

BingRoutingService.RouteRequest request = new RouteRequest();
request.Credentials = new Credentials { ApplicationId = key };

// the actual waypoints, from start to finish.
request.Waypoints = new Waypoint[]
  // Brussels
  new Waypoint(){ Location= new Location(){ Latitude = 50.8462807, Longitude= 4.3547273}},
  // Paris
  new Waypoint(){ Location= new Location(){ Latitude = 48.8566667, Longitude= 2.3509871}},

// For the international crowd, you can also switch this to miles.
request.UserProfile = new UserProfile
  DistanceUnit = DistanceUnit.Kilometer

// some optional parameters
request.Options = new RouteOptions()
  Mode = TravelMode.Driving,
  Optimization = RouteOptimization.MinimizeTime

4. Getting the results

Last thing on the to-do list is setting up our RouteServiceClient. This will send the actual request, and get the results back in a nice RouteSummary object.

RouteServiceClient client = new RouteServiceClient();

RouteResponse response = client.CalculateRoute(request);
RouteResult result = response.Result;

The distance will be in the unit specified in the UserProfile object.
The RouteSummary object will also contain the time needed to drive in seconds.

If you like you can even get the full route path from the RouteResult object, and a lot more, but I’m not going to be able to cover that in a 5 minute post 😉


11 thoughts on “Calculating Routes in .NET: 5 minutes tutorial

  1. This can not be used in website ..right ? Because when I tried it gives error “An endpoint configuration section for contract ‘BingRoutingService.IRouteService’ could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.”

    Looks like need WCF service….is the correct ?

    • Hi Bhimraj,

      as you’ve already found out: yes, you can use this code in an ASP.NET page. Adding the WCF Service adds two endpoints in your web.config, each with a different binding. Just comment or rename one of the two configurations. You will also have to increase the maxRequestSize and the maxBufferSize of the binding you’ve chosen.

  2. Pingback: Learning never exhausts the mind !!

  3. Thank you very much Koen. It worked !! I did a lot of R&D on how bing map API’s can be used in ASP.NET but didnt not find any useful information on web…not even on bing map official site…Thanks for putting this information here so nicely…

  4. Hi Koen,

    I am trying to get the maximum distance between two points. Bing Map website has two checkbox options i.e. “Avoid Highways” and “Avoid Tolls”. But these two options are not directly available in Bing Map API. Google MAP api has these options available in their API.

    Bing Map API has RouteOptions class but it does not cover these two options. Do you have any idea how we can pass/use these two options while calculating the distance using Bing MAP API.

  5. Thank you for the concise information. Do you happen to know if you can use BING to search for a city (wildcard type search) in order to get the lat/long needed to do what you are showing in your post? I need to create an application where the user enters a city name (start and end) and then calculate the distance between the known cities. The trick is I need to allow the user to lookup a city based on what they type. For example, they pick Texas and then type Dall. I need to present them with a all matches to Dall* and then the user chooses the appropriate city.

    • Hi Lee,

      Glad you like my post. To answer your question: there are numerous ways to implement this. Lots of forums have great suggestions on how to accomplish this.

  6. Hi Koen, thanks for this very useful post. i am trying to achieve the same objective through a console application. i followed the above steps and was able to call the bing api but it threw the exception
    “Credentials are either invalid or unspecified.” on the below line:
    RouteResponse response = client.CalculateRoute(request);

    i checked the Credentials property of the request object and both the ApplicationId & ApplicationIdField are set to the ApplicationID i received from the Bing website.

    i am not sure why my credentials are invalid..

  7. Pingback: [RESOLVED]Calculate the distance between tow city | ASP Questions & Answers

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s