Home > Uncategorized > Geocoding using Powershell and Google Geocoding API

Geocoding using Powershell and Google Geocoding API

It has been a while since I blogged. I guess that, sometimes, there are other things in life that need contemplation than code (and sometimes there is just not enough time for contemplation at all).

Yesterday, I sat down and played a bit with the Google Geocoding API. Being to lazy to open visual studio I started out in Powershell. It turned out really simple and I ended up with the following code:

function Get-Location($address)
    $result = Invoke-WebRequest "http://maps.googleapis.com/maps/api/geocode/xml?address=$address&sensor=false"
    if ($result.StatusCode -eq 200)
        $message = [xml]$result.Content
        if ($message.GeocodeResponse.status -eq "OK")
            return $message.GeocodeResponse.result

Get-Location expects a string containing a location description such as “Avenyn, Göteborg, Sweden” and the result is an array containing all possible interpretations along with their geographical positions.

type formatted_address address_component geometry
route Kungsportsavenyn, 411 … {address_component, ad… geometry

This script uses Invoke-WebRequest which is a new command in Powershell V3. In can be replaced simply with:

$webclient = New-Object System.Net.WebClient
$result = $webclient.DownloadString($url)

which will work in V2 (and probably in V1 too but who cares, really?).

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: