Share via


Bing Maps .NET REST Toolkit Made Open Source

Many developers use the Bing Maps REST services to perform spatial queries such as geocoding an address or calculating routes and distances. The Bing Maps REST services are nice and fast, however, using them from a .NET application used to require a decent amount of work. The Bing Maps .NET REST Toolkit aims to make it easy to use these services from your .NET application by providing a portable class library, which wraps the Bing Maps REST services and implements best practices to ensure the good performance and the most accurate results are returned. In the past, it could easily take an hour or more to add the Bing Maps REST services to their application. Now, with the aid of a NuGet package, you can implement the Bing Maps REST services in minutes. You can find this project on GitHub here.

How to use the .NET REST Toolkit

Add the REST Toolkit to your project

In Visual Studio, open the NuGet Package Manager, select the Browse tab and search for "Bing Maps REST". This will reduce the list of results enough to find the "BingMapsRESTToolkit" package. If you want to verify that you have the correct package, the listed owner of the package is bingmaps and the author is Microsoft. Install the package into your project.

Alternatively, if you are using the NuGet command line:

PM> Install-Package BingMapsRESTToolkit

Call the Bing Maps REST services using the Toolkit

The Bing Maps REST Toolkit has two key components, a service manager and a set of request classes. The ServiceManager is a static class that makes it easy to asynchronously process any Bing Maps REST request. Here is a list of the different requests classes available:

  • ElevationRequest
  • GeocodeRequest
  • ImageryMetadataRequest
  • ImageryRequest
  • ReverseGeocodeRequest
  • RouteMajorRoadsRequest
  • RouteRequest
  • TrafficRequest

The ServiceManager class has two static methods: GetResponseAsync and GetImageAsync. The GetResponseAsync method will return a Response object from the Bing Maps REST services which aligns with the documented Response object for the REST services. The GetImageAsync method will return a stream containing the image data.

The following is an example of how to make a geocode request and get the response from the Bing Maps REST services.

//Create a request.var request = new GeocodeRequest(){Query = "New York, NY",IncludeIso2 = true,IncludeNeighborhood = true,MaxResults = 25,BingMapsKey = "YOUR_BING_MAPS_KEY"};//Process the request by using the ServiceManager.var response = await ServiceManager.GetResponseAsync(request);if(response != null &&response.ResourceSets != null &&response.ResourceSets.Length > 0 &&response.ResourceSets[0].Resources != null &&response.ResourceSets[0].Resources.Length > 0){var result = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;    //Do something with the result.}

The following is an example of how to request a map image from the Bing Maps REST services to retrieve the image stream.

//Create an image request.var request = new ImageryRequest(){CenterPoint = new Coordinate(45, -110),ZoomLevel = 12,ImagerySet = ImageryType.AerialWithLabels,Pushpins = new List<ImageryPushpin>(){new ImageryPushpin(){Location = new Coordinate(45, -110.01),Label = "hi"},new ImageryPushpin(){Location = new Coordinate(45, -110.02),IconStyle = 3},new ImageryPushpin(){Location = new Coordinate(45, -110.03),IconStyle = 20},new ImageryPushpin(){Location = new Coordinate(45, -110.04),IconStyle = 24}},BingMapsKey = "YOUR_BING_MAPS_KEY"};//Process the request by using the ServiceManager.using (var imageStream = await ServiceManager.GetImageAsync(request)){//Do something with the image stream.    //Here is how to display the image in an Image tag in a WPF app.var bitmapImage = new BitmapImage();bitmapImage.BeginInit();bitmapImage.CacheOption = BitmapCacheOption.OnLoad;bitmapImage.StreamSource = imageStream;bitmapImage.EndInit();MyImage.Source = bitmapImage;}

Bing and Open Source

The Bing team has been working towards being more involved in the Open Source community. Here are a few other open source projects recently released by the Bing team that you may be interested in.