Bing Maps REST Service .NET Libraries

Bing Maps has two main services for geocoding and routing on the fly. These are the Bing Maps SOAP and Bing Maps REST services. The SOAP services were released before the REST services and have traditionally been the preferred service when working with the .NET framework. The REST services have a lot more features and functionalities than the SOAP services but have been a bit harder to work with in managed code. In addition, many people who have used the REST services with managed code have preferred to work with the XML responses rather than JSON as they are easier to manually parse. This results in losing out on one of the key reasons for using the REST services, smaller responses. There has been some documentation on how to use the REST services in managed code however the it only shows how to handle geocode requests. This post will expand upon that documentation and point you in the right direction to handle the rest of the API’s in the Bing Maps REST services.Note that the Bing Maps REST services have new features and functionalities added to them on a regular basis, the material provided in this blog may become outdated and will not automatically be aware of any new feature in the service.

What I have done is gone through all the documentation for the Bing Maps REST services created a set of C# class definitions that have the same structure as the JSON responses. Note that the XML responses are different and will not work with these classes. In order for these classes to work in your application you will need to add a reference to System.Runtime.Serialization and System.ServiceModel.Web (for Silverlight applications). Doing some tests I have found that these class work great with the Bing Maps WPF, Silverlight, and WP7 map controls. They also work great in console apps as well although a bit hard to visualize that data.

Now on to the fun stuff, I’ll leave the class definitions to the end and first give some examples of how to use them. To start you will want to write some code to query the Bing Maps REST services. This can easily be done using WebClient or a HttpWebRequest. You will then just need to create a query URL to access the service. Once this is done you can take the response from WebClient or HttpWebResponse and serialize it with the Bing Maps REST service class definitions. This can be done using the DataContractJsonSerializer class. Here is an example of a asynchronous method that works well in WPF, and Silverlight:

I have not tested it with the metro apps but there is no reason why this shouldn’t work as it works in all the other flavour of .NET apps such as WPF, Silverlight, and standard console type applications.

This can be done although it would require a decent amount of math. Most of the math you will need can be found here: http://msdn.microsoft.com/en-us/library/bb259689.aspx The key thing to note is for every zoom level a pixel represents about twice the distance as the previous zoom level. Since you are working with a floor plan the distances will be relatively small so you won’t need to worry too much about the curvature of the earth and can use flat 2D math once you translate your reference point and resolution.

I’m assuming all your data was created at a specific resolution (1 m/pixel). Based on this I would suggest the following. At a zoom level calculate the ground resolution of the map. Use this to scale your data to fit this resolution. (should be a simple multiplication). Then use the LatLong to Pixel method in the link I provided on your reference point to get the global pixel coordinate of your reference point. Then use this global pixel as an offset to your data to get global pixel coordinates for your data. Then use Pixel to latlong function to convert this data into usable coordinates that can be overlaid on the map.

Simply use those as way points for a route. If you have more than 25 points you will need to calculate multiple routes. If the data you have is already the hi-definition line of a route then simply overlay it on the map as a Polyline.

I am using the above URL to get 3 different routes from my origin to destination. My question is that when I get the result, is there any way we can determine whether the route was calculated based on traffic or not etc?

I am using the above URL to get the route info (time, distance etc). I want to find the travel time and distance from 5 different points to the same destination. Let’s say I want to find the travel time and distance from [San Francisco to Oakland], [San Jose to Oakland] , [Sunnyvale to Oakland]. as you can see, my destination is same but origin is different in each request. Right now I am making 3 different calls to Bing. I would like to reduce the call to Bing. So is there anyway, I can call Bing Map only once (instead of 3) to get 3 different route data?

For your first question, unless you have set the optimization property in the request to use time with traffic the routes will be calculated to optimize for shortest driving time, assuming clear roads. If you want use traffic then add the following you your request: &optimize=timeWithTraffic

For your second question, what you can do is group the route calculations together into a single, multi-pint route request that goes back and fourth between your main location and all other points. For example: [Oakland to San Francisco to Oakland to San Jose to Oakland to Sunnyvale to Oakland]. You can then extract the time and distances from the route legs. In the example the first, third and fifth route legs will be the ones that start in Oakland and go to other cities. The second, fourth and sixth route legs will be the routes heading to Oakland.

Ricky, Thanks so much for your quick reply. Really appreciate it. Could you please give me an example (sample URL request include multi-point into single url) url request which include multi-point as single request, so that I could construct the URL. Thanks so much.

The generated route will have several route legs. Each route leg has travel time and distance properties which contain the information you are looking for. Simply grab this information from all the odd route legs to get the information where Oakland is the starting point. Use the odd route legs to get the information where Oakland is the destination.

I’m new to this and seem to be massively missing the basics. How do I use this to actually get the map on the screen if used in a C# .net (Lightswitch silverlight) application. My application does not use xmal pages as for as I know.

The REST services are primarily for requesting raw data; geocoding, routing, elevation data. You can get a static map image if you want as well. If you want an interactive map you have to use one of the interactive Bing Maps controls. There is a Silverlight control, but it is nearing end of life (so is Silverlight in general). Here are some useful resources on using Bing Maps with Lightswitch: