Bing Maps .NET REST Toolkit Made Open Source

NOTE: An updated version of this article can be found in the Microsoft Maps Blog.
https://www.microsoft.com/en-us/maps/news/bing-maps-net-rest-toolkit-made-open-source

Bing Maps .Net REST ToolkitMany developers use the Bing Maps API's 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 makes it easy to use these services from your .NET application by providing a portable class library. This library 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 .NET 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 .NET Toolkit

The Bing Maps .NET 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 API REST service from your .NET app.

//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 API REST services to retrieve the image stream with the .NET toolkit.

//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 Maps API team has been working towards being more involved in the Open Source community, and open source REST service support for .NET apps is just one of many examples. Here are a few other open source projects released by the team that you may be interested in.

We’re always working to help developers push the envelope with intuitive mapping solutions and useful resources. Create a free Bing Maps API key to build your next mapping app on a robust platform.