<

Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Geocoding addresses using Bing Maps (examples in PowerShell)

Published on
12,398 Points
6,098 Views
3 Endorsements
Last Modified:
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
Comment
Author:rob_farley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
0 Comments

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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