Community Pick: Many members of our community have endorsed this article.

Geocoding addresses using Bing Maps (examples in PowerShell)

Rob FarleyConsultant
CERTIFIED EXPERT
SQL MVP, MCM, MCT. Presenter. Business owner.
Published:
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.

I've written about this stuff before on my blog at http://sqlblog.com/blogs/rob_farley/archive/2010/05/23/fetching-latitude-and-longitude-co-ordinates-for-addresses-using-powershell.aspx, but articles here are a lot more accessible for EE users.

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.

Yes, I said web service. That's how we do it.

In PowerShell, I'm going to use:
$ws = New-WebServiceProxy -uri http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl;

Open in new window


...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:
$wsgr = new-object Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1ervice_geocodeservice_svc_wsdl.GeocodeRequest;

Open in new window


...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.
$wsgrc = new-object Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1ervice_geocodeservice_svc_wsdl.Credentials;
                      $wsgrc.ApplicationId = $key;
                      $wsgr.Credentials = $wsgrc;

Open in new window


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!):
$wsgr.Query = 'Adelaide, Australia'; 

Open in new window


This address can be a street, etc, but here I'm just using the city of Adelaide, where I live.

And then you make the call, getting some results back.
$wsr = $ws.Geocode($wsgr); 

Open in new window


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].
$wsr.Results[0] | select {$_.Address.FormattedAddress}, {$_.Locations[0].Longitude}, {$_.Locations[0].Latitude};

Open in new window


And there we have it. This command will have given me results a bit like this:
Adelaide, Australia          138.599731698632          -34.925769791007

Open in new window


You can dump this into a database or whatever you like.

It's just too easy...
3
10,786 Views
Rob FarleyConsultant
CERTIFIED EXPERT
SQL MVP, MCM, MCT. Presenter. Business owner.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.