Where Am I? - Using Google Geocoding API with Perl

Quite some time ago I stumbled across a similar blog post to this one which detailed how to access Google Maps' geocoding API to find latitude and longitude coordinates from a street address.

The process is extremely simple, and I just thought I’d give a quick overview of how the whole thing works.

So to start off with, the base URL for the API service we’ll be using is http://maps.google.com/maps/api/geocode/json?sensor=false&address=

As you may be able to tell from a quick glance at the above URL, this will return a JSON encoded string containing the details of the place you’re looking for (if the almighty Google servers managed to find such a place). If XML is more your style, simply substituting the “json” for “xml” in the URL should suffice, but I love the simplicity and ease of use that JSON offers, thus my following examples will be related to the JSON flavour of the geocoding API.

So a simple perl script to find “Southern Cross Station, Melbourne” could look something like this:


use strict;
use warnings;

use LWP::Simple;
use URI::Escape;
use JSON;

my $address = "Southern Cross Station, Melbourne";

my $geo_url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";
my $url = $geo_url. uri_escape($address);

# GET and decode the JSON response from Google Maps
my $response = get($url);
my $json = decode_json($response);

# Parse out some relevant data
my $location = $json->{results}[0]->{geometry}->{location};
print "Latitude: ", $location->{lat},
    "\tLongiude: ", $location->{lng}, "\n";

Ok great, so I’ve hard-coded the address, and only used a small portion of the response given by the maps API server, but as you can see (if you run this script), it should return the coordinates of the station as expected. In my test I received

Latitude: -37.8181602  Longiude: 144.9533883

Now, conversely you are able to get the street address from a set of {lat,long}itude points by substituting them where we used the “Address”.

As you can see,

my $location = $json->{results}[0]->{geometry}->{location};

$location is being set to only a small subset of what is actually being returned. Should you have wanted to get the street address, then something along the lines of


would have given a nicely formatted street address.

Specificity is also key here, as obviously there may be more than one “Southern Cross Station” in the world. When looking for street addresses try to specify state/suburb or even postcode where possible so as not to get false positives, or try filtering out dud results if there are more than one returned.

If you’re interested, I strongly recommend taking a look at this nifty feature from Google. Make a few requests and take a look at the whole response to see the other good stuff that gets returned, get a feel for the whole process and go nuts with some geomagick.

A few sidenotes.. Google will limit the amount of requests per server/IP per day to around 2,500. As with most things, YMMV.

Check out https://developers.google.com/maps/documentation/geocoding/ for the full run down.

Categories: Tech
Originally published at: http://texh.net/blog/using-googles-geocoding-api-with-perl
Next Post ›