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 ๐Ÿ˜‰


STM.NET and .NET Framework 4.0 beta 2 ( Error 1603: A fatal error occurred. )

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

Recently I ran into some problems installing the latest Visual Studio 2010 beta 2. It gave me the very clear error message:

“Error 1603: A fatal error occurred.”

Well that about says it all, right…?

After a lot of googling and frustration, it seemed that my previous installation of STM.NET caused this error.

I should have remembered that ย “the STM enabled version of Microsoft .NET version 4.0 and official versions of .NET
version 4.0 are mutually exclusive, and cannot be installed simultaneously on the same computer

Uninstalling STM.NET was not as easy as I thought. I assumed the almighty internet would have answers, but no dice. I spent ย about a day and a half of trying almost everything : .NET Framework Cleanup tools, uninstalling anything with ‘.NET or ‘Visual’ in it’s name, registry cleaning, manually removing folders from the Windows folder ย ( I got desperate ).

Finally, I found the answer, thanks to an old Microsoft Support article.

Following the next steps should help you remove the STM.NET version of .NET Framework 4.0:
Uninstall NET Framework 4 STM

  1. Go to : Start > Run > type ‘Installer’, press enter
  2. Right click on the column header and add the ‘Comments’ column
  3. Sort alphabetically and search for ‘Microsoft .NET Framework 4 Client Profile Beta 1 enabled to use Software Transactional Memory…’
  4. Right-click and uninstall!
  5. Reboot … just in case =)

This should enable you to succesfully install the ‘regular’ .NET Framework 4 Beta 2 and enjoy the new Visual Studio 2010 Beta!