Jump to content

Counting distance between 2 gps points


ts2000abc

Recommended Posts

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'	=> 52.516074,		//	Berlin, Germany
	 'longitude'	=> 13.376987
	   );
$endPoint = array( 'latitude'		=> 59.332175,		//	Stockholm, Sweden
	'longitude'	=> 18.062435
	 );

$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 />';

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.