Jerry Nixon @Work: Easily include Bing Map data in your Windows 8 Metro App

Jerry Nixon on Windows

Monday, June 18, 2012

Easily include Bing Map data in your Windows 8 Metro App

imageThere’s no question how awesome Bing Maps is. It is loaded with innovation and tons of awesome data that now you can include in your application by using their REST based services. The services provide a lot of value to developers and their apps including data source management:

New: Bing Maps Account Center now provides the ability to manage data sources created by Data Source Management API (Bing Spatial Data Services). You can now use the Manage my data sources page to view and edit all the data sources associated with your account in one place.

But the core of the Bing Maps service is the API.

There are four API services:

  1. Locations API - Use the Locations API to geocode and reverse-geocode location data.
  2. Imagery API - Use the Imagery API to get a static map and imagery data information such as map tiles and providers.
  3. Routes API - Use the Routes API to get directions and route information for driving, walking or using transit.
  4. Traffic API - Use the Traffic API to get information about traffic incidents and issues in a specified area.

This article will be using the Locations API specifically. Using this we can leverage data from other services like Bing’s Search and plot them on a map control.

The are three Location API queries:

  1. Find a Location by Address Use these URL templates to get latitude and longitude coordinates for a location by specifying values such as a locality, postal code, and street address.
  2. Find a Location by Point Use this URL template to get the location information associated with latitude and longitude coordinates.
  3. Find a Location by Query Use these URL templates to get latitude and longitude coordinates that correspond to location information provided as a query string.

This article will implement all three of these methods – yep, all of them. Each has their own use and implementing just one would be a sick waste of time. So here it goes.

First, get a key

The Bing Maps Portal is where you start. A developer key is quick, simple, and free. In fact, using the Bing Map services is completely free. That’s great. Once you have your key, we can really get started.

image

Second, copy my helper

It never gets old for things to be easy. I created this helper to be helpful. You might want to use it. You might want to copy it. You might want to amend it. I don’t care what you do with it. I just hope it helps a little – and that you send me a note letting me know it worked for you.

Get the code here. (Just copy it to your project.)

Here’s the Helper’s API:

Constructor(s)

BingApiV2Helper(key);

Example:

var _Helper = new BingSearchHelper(“youneedyourownkey”);

Method - FindLocationByPointAsync

FindLocationByPointAsync(double latitude, double longitude);

Example:

var _Result = new FindLocationByPointAsync(0d, 0d);

Method - FindLocationByQueryAsync

FindLocationByQueryAsync(string query, int maxResults = 5);

Example:

var _Result = new FindLocationByQueryAsync(“coffee”);

Method - FindLocationByAddressAsync

FindLocationByAddressAsync(Countries country = Countries.US, string adminDistrict = null, string locality = null, string postalCode = null, string addressLine = null, string userLocation = null, string userIP = null, int maxResults = 5);

Example:

var _Result = new FindLocationByAddressAsync();

That’s it. The helper is just a method call away and, presto, you have the data you want thanks to the Bing Maps REST service. It’s worth pointing out that there’s an SDK, too. It’s up to you how you access the data. But if your needs are simple, my helper might be all you ever need. Otherwise, Bing Maps has a forum (here) in case you hit any issues.

Conclusion

For all you lawyers out there, you will likely want to check out the Terms of Use specifically for Windows 8 Metro applications. In particular, read this:

image

The reality is, those terms are not very restrictive. And Bing Maps is a powerful platform to leverage. If, for some reason, Bing Maps doesn’t suit you. Consider Yahoo! geocoding services – tey also integrate into WinRT applications easily. It’s not as powerful, sure, but it’s cool to have options.

Best of luck!