Jump to content

PostCode problem


paulman888888

Recommended Posts

I don't know where to start with this. I have a database of postcode, longitude and latitude and location name.

 

But what i want to do now is a distance between two codes,

i thought of getting both posts longitude and latitude and doing some calculations but i don't know what sort of calculations i would do.

 

But then i thought about a radius search and i thought about getting the starting postcode

and erm. Stuck.

 

Any ideas?

 

This isn't a PHP question really because i could make it when i find out the relation ship between longt and lati and miles.

 

If you understud what i am asking (1st of all your smart because i've said everything in a strange way) can you help

 

Thankyou all

Paul

Link to comment
https://forums.phpfreaks.com/topic/160127-postcode-problem/
Share on other sites

function calculateDistanceFromLatLong($point1,$point2,$uom='km') {
//	Use Haversine formula to calculate the great circle distance
//		between two points identified by longitude and latitude
switch (strtolower($uom)) {
	case 'km'	:
		$earthMeanRadius = 6371.009; // km
		break;
	case 'm'	:
		$earthMeanRadius = 6371.009 * 1000; // km
		break;
	case 'miles'	:
		$earthMeanRadius = 3958.761; // miles
		break;
	case 'yards'	:
	case 'yds'	:
		$earthMeanRadius = 3958.761 * 1760; // miles
		break;
	case 'feet'	:
	case 'ft'	:
		$earthMeanRadius = 3958.761 * 1760 * 3; // miles
		break;
	case 'nm'	:
		$earthMeanRadius = 3440.069; // miles
		break;
}
$deltaLatitude = deg2rad($point2['latitude'] - $point1['latitude']);
$deltaLongitude = deg2rad($point2['longitude'] - $point1['longitude']);
$a = sin($deltaLatitude / 2) * sin($deltaLatitude / 2) +
	 cos(deg2rad($point1['latitude'])) * cos(deg2rad($point2['latitude'])) *
	 sin($deltaLongitude / 2) * sin($deltaLongitude / 2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$distance = $earthMeanRadius * $c;
return $distance;
}	//	function calculateDistanceFromLatLong()


$startPoint = array( 'latitude'		=> 51.5226,		//	Baker Street
				 'longitude'	=> -0.1571
			   );
$endPoint = array( 'latitude'	=> 51.4827,			//	Cutty Sark
			   'longitude'	=> -0.0096

			 );

$uom = 'km';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

$uom = 'm';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

$uom = 'miles';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

$uom = 'yds';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

$uom = 'ft';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

$uom = 'nm';
$distance = calculateDistanceFromLatLong ($startPoint,$endPoint,$uom);
echo $distance.' '.$uom.'<br />';

Link to comment
https://forums.phpfreaks.com/topic/160127-postcode-problem/#findComment-844870
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.