Posts Tagged ‘UK Postcodes’

Google Maps API part 1: Accurate Geocoding for UK Postcodes


Recently I had to build a system that would take an address, pass it to google and return latitude and longitude values. The result is then stored for later use and display on Google maps.

However, firstly I found that the default XML geocoding service provided by Google that allows you to do bulk geocoding on a data source and the geocoding provided by their maps API/ AJAX library (GClientGeocoder) had huge differences in results. Most postal codes passed to googles XML geocoding service were miles out or just not recognised. Yet when using the same postcode in the maps API when calculated on the fly the results were more or less spot on.

You can take a look at a tutorial for using Googles XML geocoder service, PHP and MySQL here. They suggest that this way is good if you don’t want to repeatedly geocode the same address again and again and if you want to do bulk geocoding.

Which would be nice, if it was actually accurate! But this still leaves me with having to do bulk uploads or geocoding and get accurate results to build, for instance, a store locator around the data.

The solution is simple, we can utilise the Google maps API to do the geocoding for us on the fly and store the result passed back using some simple AJAX to send the data to PHP/ MySQL. While I geocode my records at the time of their entry, in the example below, you could just as easily use this to create some sort of bulk update. Check out my tutorial here about using AJAX to pass form data to PHP if you’re new to this.

Firstly you will need to signup to get a couple of free API keys from google, you don’t require much to get them and they’ll work just fine for a localhost.

Maps API key:
AJAX Search API key:

OK so now we create a HTML page, form.htm, this will call in the Javascript libraries and do the geocoding work.

We also build a function that creates an XML object in the DOM to store the data, which will then get passed to the PHP page. This works in exactly the same way as my tutorial here.

This chunk of code above is what utilises the google maps keys, to pass the data to google, geocode it and return the record and I've written this inside of a function that gets called on the form submission, which in turn then uses the previously declared XML creation function.

The final stage of this HTML page is of course the form which sends the data and a span tag that displays the results if successful.

Address Line 1:
Address Line 2:
Address Line 3:


This then just leaves us the PHP page (post.php) that will then post the results, again nothing too disimilar to my previous tutorials on this. This is the bit where you would pass in the $_POST parameters and store them using MySQL etc... or save them to a text file.

And there we have it, accurate geocoding from google that can be used as a service when data is entered, or you could use this same method by passing in XML created from your database and iterating over this passing each result to the API and returning a value.

Now you can build a store locator or whatever that actually returns accurate results, also you can then use this to integrate your database better with the full Google maps API - showing maps, placing markers on maps etc..