[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Geocoding addresses using Bing Maps (examples in PowerShell)

Published on
13,876 Points
3 Endorsements
Last Modified:
Community Pick
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...

Featured Post

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Join & Write a Comment

This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month