Forum Moderators: buckworks

Message Too Old, No Replies

obtain UK postcode distance information

Want to identify distance from Postcode A to postcode B

         

hughie

6:17 pm on Feb 15, 2005 (gmt 0)

10+ Year Member



I am building a site that would benefit greatly from an "Autotrader" style search within 10/20/50 miles of my postcode.

Is this information available somewhere or is it a case of me buying a database of postcodes and doing all the leg-work.

Cheers,
Hughie

hughie

11:55 pm on Feb 15, 2005 (gmt 0)

10+ Year Member



No Worries, got it sorted

figured it thanks to a database of postcodes containing grid references and a bit of good ole pythagorus

Hughie

etrader

9:38 am on Feb 16, 2005 (gmt 0)

10+ Year Member



Hi, I am interested in such a database.

cheers

[edited by: lorax at 1:16 pm (utc) on Feb. 16, 2005]

hughie

7:18 pm on Feb 25, 2005 (gmt 0)

10+ Year Member



Quite a few people have been mailing me for my solution to this one so here is a simplified version:

you'll need the database of postcodes, sticky me for a link to the sql file but the table should go something like:

Pcode, Grid_N, Grid_E
BD1 416300 433300
LE7 463000 309300

whack this in a php file:


<?php

// Connect to the database server
$dbcnx = mysql_connect("localhost",
"user", "password");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time (this is a host connect problem).</P>" );
exit();
}

// Select the database
if (! mysql_select_db("dbasename", $dbcnx) ) {
echo( "<P>Unable to locate the " .
"database at this time(this is a dbconnect problem).</P>" );
exit();
}

function calc_postcode_seperation($pcodeA,$pcodeB)
{
// PCODE A
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$pcodeA' LIMIT 1");
$row=mysql_fetch_array($result);
$gridn[0]=$row[Grid_N];
$gride[0]=$row[Grid_E];

// PCODE B
$result=mysql_query("SELECT * FROM postcodes WHERE Pcode='$pcodeB' LIMIT 1");
$row=mysql_fetch_array($result);
$gridn[1]=$row[Grid_N];
$gride[1]=$row[Grid_E];

// TAKE GRID REFS FROM EACH OTHER.
$distance_n=$gridn[0]-$gridn[1];
$distance_e=$gride[0]-$gride[1];

// CALCULATE THE DISTANCE BETWEEN THE TWO POINTS
$hypot=sqrt(($distance_n*$distance_n)+($distance_e*$distance_e));

$text.='Distance between '.$pcodeA.' and '.$pcodeB.' is: '.round($hypot/1000,2).'kms';
return $text;

}

echo calc_postcode_seperation('LE7','BD1');

?>

Hope that is of some help to people

hughie