Where you headin’, luv?
In previous posts I’ve covered the inherent difficulty of geocoding addresses and postcodes in the UK, specifically in order use the geocodes with Google maps. I learned a lot about the limitations of Google’s various geocoding services. To sum up the situation:
- The Google maps API’s geocoding webservice is buggy: giving more address details can give a less accurate, and in many cases plain wrong, geocode. However, the GClientGeocoder class built into Google maps fares a lot better with addresses…
- … apart from when the address entered is a postcode. Then it geocodes to the central point of the postcode sector, rather than the precise postcode location, i.e if you search for E3 4GY or E3 4NT, it will return the geocode for the centre of E3 4 in both cases.
- But, as I discovered when trying an alternative route to postcode geocoding, the Google Ajax search API, the localSearch class in particular, gives very accurate geocodes for full postcodes.
Previously I’d been geocoding large batches of addresses/postcodes, but having got the geocodes in order to plot points on the map, I wanted to add search functionality to the map, so that it would zoom in on a given address/postcode. I wanted it to be accurate for both addresses and postcodes, so using what I’d learned, I wrote a javascript function, geocodeUKAddress, which always returns the best geocode that Google can offer, so your website can be as reliable as a London cabbie (again, I can’t embed it here as javascript has a habit of breaking wordpress, though there must be some way to make it safe – I will research).
You will need to include the following in ther head of your webpage too
<script src="http://maps.google.com/maps?file=api&v=2&key=your_api_key_here" type="text/javascript"></script> <script src="http://www.google.com/jsapi" type="text/javascript"></script>
I can’t take credit for the regular expression that recognises UK postcodes, but apart from that, it’s all my own work, which anyone can feel free to use. Unless you maybe work for Rupert Murdoch.
Related posts:
- Anarchy in the UKThis damn economic crisis/swine flu outbreak isn’t quite that bad...
- Geocoding in the UKThe art of geocoding addresses in the UK, , is...
- What’s up, firebug?In the space of one day (in real-time, not blog-post-spacing...
- How to spot an obvious idea when you’ve got one, and I quote: Now, somebody has probably already done...
- Better jQuery plugins searchEveryone wants one and, let’s face it, anyone cool already...
Tags: accurate, address, api, free, function, geocoding, google ajax search, google maps, postcode, string, uk