Converting UK PostCode To Longitude And Latitude With PHP

×

Error message

Warning: apc_store(): Potential cache slam averted for key 'www.hashbangcode.com::cache_bootstrap::variables' in DrupalAPCCache->set() (line 206 of /var/www/www.hashbangcode.com/2014-03-22-2105/docroot/sites/all/modules/contrib/apc/drupal_apc_cache.inc).

Friday, November 21, 2008 - 14:55

This common problem has stumped many programmers in the past, so I thought I would add in my little part. Whilst doing research for this I manged to find a site called www.streetmap.co.uk which has a nice little PostCode to geographical reference tool. Using a simple URL parameter I was able to give the site a PostCode and strip the longitude and latitude from the resulting HTML. Here is the function I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function postCode2Geog($code){
 $code = strtolower(str_replace(' ','',$code));
 $uri = "http://www.streetmap.co.uk/streetmap.dll?GridConvert?name=".$code."&type=Postcode";
 
 $ch = curl_init($uri);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $output = curl_exec($ch);
 curl_close($ch);
 
 preg_match('#long</strong> \(wgs84\)\s*?\<\/td\>\s*?\<td width="50%" align="center" valign="middle"\>\S{2,3}:\S{2}:\S{2} \( (.*?) \)#i',$output,$longMatch);
 preg_match('#lat</strong> \(wgs84\)\s*?\<\/td\>\s*?\<td width="50%" align="center" valign="middle"\>\S{2,3}:\S{2}:\S{2} \( (.*?) \)#i',$output,$latMatch);
 $long = $longMatch[1];
 $lat = $latMatch[1];
 
 return array($lat,$long);
}

To use the function use the following, in this case I am looking at the BBC studio in London.

$geog = postCode2Geog('W1A 1AA');

This produces the result of 51.518561 latitude and -0.143800 longitude, which seems to check out.

Initial results from using this method seem promising, but the Streetmap site seems not to have been updated since 2004. I found this method after a few minutes searching, and I only needed to convert 20 or so postcodes into geographical coordinates. An alternative is to use a method that I have talked about previously in the post find longitude and latitude of PostCode or ZipCode using Google Maps And PHP, but that requires you to have a valid, working Google maps API key. This method is an alternative if you don't want to go down that route.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

Fatal error: Call to undefined function curl_init() in C:\wamp\www\RICHARD-3\viewbusiness.php on line 12

CURL is enable in my server. Please help

philipnorton42's picture
Submitted by philipnorton42 on Thu, 05/09/2013 - 12:44

CURL might be installed but what about the CURL-PHP extension? Check for your phpinfo() output.

Actually, you seem to be running a Windows machine so you should go into your php.ini file and make sure that the php_curl.dll extension is enabled.

Add new comment