4

I have a PHP script at the moment which allows users to enter a UK postcode.

I was wondering if there was a way I could get their geolocation from the postcode they enter. (Through Google's API or something?)

I have tried http://code.google.com/apis/maps/documentation/geocoding/index.html but it requires a full address, whereas I only have a postcode.

Help would be appreciated, thanks :) Peter

Peter Stuart
  • 2,362
  • 7
  • 42
  • 73

5 Answers5

7

There's now a free UK government alternative to the other options listed here. Go to http://postcodes.io/ to see the details of the API and examples

MarkG
  • 1,859
  • 1
  • 19
  • 21
5

Here is a downloadable database for all UK postcodes with latitude and longitude.

http://wikileaks.org/wiki/UK_government_database_of_all_1,841,177_post_codes_together_with_precise_geographic_coordinates_and_other_information,_8_Jul_2009

Here is Step by Step tutorial for Geocoding UK Postcodes with Google Map API

http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/

Tomi
  • 245
  • 1
  • 8
  • 3
    Rather than using wikileaks for this (as though it were something illicit and "under the counter") why not simply use the Ordnance Surveys Open CodePoint dataset - https://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open/index.html - and it's more up-to-date than wikileaks 2009 – Mark Baker Jan 31 '12 at 14:46
3

Ordnance Survey's Open CodePoint dataset of postcodes gives you a dataset of all UK mainland postcodes

https://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open/index.html

You can then convert these to WGS84 Lat/Long using a library like this

http://www.jstott.me.uk/phpcoord/
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
2

You could check out the Yahoo PlaceFinder API

Heres an example URL http://where.yahooapis.com/geocode?q=BL12DD&appid=[yourappidhere]

I have used this in the past and found it to be somewhat accurate.

Docs: http://developer.yahoo.com/geo/placefinder/

Heres come code

<?php

    $data = fetchPage("http://where.yahooapis.com/geocode?q=BL12DD&appid=%5Byourappidhere%5D&flags=J");
    $yahooData = json_decode($data);

    echo '<pre>'.print_r($yahooData, true).'</pre>';

    echo '<br />Lat: ' . $yahooData->ResultSet->Results[0]->latitude;

     function fetchPage($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

?>
Damon Skelhorn
  • 1,491
  • 11
  • 18
  • tHE LINK YOU PROVIDED SEEMS TO BE ACCURATE, HOW CAN i TIE IT IN WITH MY php SCRIPT, DOES YAHOO HAVE A PAGE FOR THAT? – Peter Stuart Jan 31 '12 at 14:51
  • PHP doesn't seem to like the function "fetchpage"? is this a standard function or will I need to install a PHP module? – Peter Stuart Jan 31 '12 at 15:09
  • I got it! I simply changed fetchPage to get_file_contents and it echo'd the latitude variable you used in the example. The CURL functions weren't need. But thanks alot Damon! :) – Peter Stuart Jan 31 '12 at 15:11
0

Though this is already answered: here is a link for a simple API that allows obtaining GeoJson for googlemaps..query by single or combining UK Postal Code(ex. ZE1 0AE) ,Sector, District, City, and Wards Boundaries

https://www.mashape.com/vanitysoft/uk-boundaries-io

query by uk postal

Jeryl Cook
  • 989
  • 17
  • 40