views:

565

answers:

3

What is the most effective and accurate way to geocode a UK postcode? Using the built in geocode object results in massively blurred results (lots of postcodes returning just a general area).

Are there any free UK postcode geocoding services I can plug into via JavaScript?

+3  A: 

Edit: To be clearer this uses the local search in google search api, not the google maps api to do the geocoding which is much more accurate.

The best way is to use Google Search Api.

If you get an api key and get it all setup: http://code.google.com/apis/ajaxsearch/

var localSearch;
var map;
var icon;
var addressMarkerOptions;
google.load("search", "1");
google.load("maps", "2");
google.setOnLoadCallback(initialize);

function initialize()
{
  localSearch = new GlocalSearch();
  icon = new GIcon(G_DEFAULT_ICON);
  addressMarkerOptions = { icon:icon , draggable: false};
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  plotAddress("OX4 1FJ");
}

/**
* This takes either a postcode or an address string
*
*/
function plotAddress(address)
{
  localSearch.setSearchCompleteCallback(null, 
    function() {

      if (localSearch.results[0])
      {     
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        var marker = new GMarker(point, addressMarkerOptions);
        map.addOverlay(marker);
        map.setCenter(point, 5, G_NORMAL_MAP);
      }
      else
      {
        alert('address not found');
      }
    });

  localSearch.execute(address + ", UK");
}
johnwards
I think the OP is finding that using the Google maps API yields very hazy results for UK postcodes. I personally have found the API to yield different results for a postcode than the results observed from the google maps website.
Paul Suart
This isn't using the google maps api to do the geo coding. Its using the local search api which is much more accurate. I'll edit this to be clear.
johnwards
+1 brilliant, exactly what I was after worked superbly thank you! Are there restrictions on how many requests you can make in one day? I'd used this before but couldn't remember anything about it, but I recall there being a limitation of some kind? Anyway, thanks again, superb!
joshcomley
No idea on the restrictions, but it will be on the google api site somewhere.
johnwards
A: 

I believe Live/Bing/Windows maps' API gives a much more accurate placement for a UK postcode than the Google maps API does.

Alternatively, there are databases of UK postcode data available, although they're quite costly. There are some interesting comments on this post: http://jamiethompson.co.uk/web/2008/07/24/full-uk-postcode-database-for-free/

Paul Suart
Additionally, there's a XML, JSON, JSONP webservice http://jamiethompson.co.uk/projects/2010/04/30/an-open-free-uk-postcode-geocoding-web-service/The JSONP flavour being ideal for plugging directly into a JavaScript app.
WibblePoop
A: 

Ordnance Survey have made their data free - http://www.ordnancesurvey.co.uk/oswebsite/opendata/ - you could cook up your own web service ;-)

Adam