You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

35 lines
1.0 KiB

<?php
class geocode
{
/**
* straight_line_distance - Returns Straight Line distance between two sets of (LAT/LONGS)
* @param real $lat1 - Latitude of Location #1
* @param real $lon1 - Longitude of Location #1
* @param real $lat2 - Latitude of Location #2
* @param real $lon2 - Longitude of Location #2
* @param string $unit - Unit of measure [DEFAULT = statute miles] ('K'=kilometers, 'N'=nautical miles)
* @return numeric - returns the distance in the desired unit of measure.
*/
public function straight_line_distance($lat1, $lon1, $lat2, $lon2, $unit='')
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K")
{
return ($miles * 1.609344);
}
else if ($unit == "N")
{
return ($miles * 0.8684);
}
else
{
return $miles;
}
}
}