Our community of experts have been thoroughly vetted for their expertise and industry experience.
SQL MVP, MCM, MCT. Presenter. Business owner.
Published:
Browse All Articles > Geocoding addresses using Bing Maps (examples in PowerShell)
I feel like more and more people want to know how to programmatically convert addresses into geospatial locations. So in this article, I will show you how you can do it with Bing Maps. I'm going to use PowerShell, which is a nice scripting language, but you can easily transform the information here into your programming language of choice.
The first thing to do is register for an account at http://www.bingmapsportal.com - the price will depend on what you want to do with the data, and many uses are free. Once registered, you'll get a Key. This key is what you'll use as a Credential in the Bing Maps Silverlight control, and you'll also use it for accessing the web service for Geocoding.
...and that URI is what you can use from within Visual Studio, or whatever other system you choose.
If you look at the look at the generated web service proxy, you'll notice that one of the available operations is called Geocode(), and that it takes a GeocodeRequest object as a parameter. In PowerShell I can create that using:
...although this is very ugly and verbose in a way that you don't have to put up with if you're not scripting. In Visual Studio it's far more elegant, as the GeocodeRequest is one of the classes that become available from your Web Reference.
You also need to tell it about that key you had, using the Credentials class that also came with the proxy. That key goes into the ApplicationId property.
And that's just about it. Now we're ready to start converting addresses.
First I populate an address into the GecodeRequest object (which can even contain typos - try this on your own data, but misspell your street or city. You'll see it corrects it for you in the results. Data cleansing on addresses!):
This object contains a Request array, which includes a better Address object, complete with each part of the Address (and my favourite, a FormattedAddress), and other things like the Latitude and Longitude. It's an array in case there are multiple results returned. But I just want the first element, so I use [0].
Comments (0)