The haversine formula is pretty useful in calculating this distance, and since the Earth is almost a sphere, it can provide a good approximation of distance between two coordinates. Enjoy!
function greatCircleDistance($latA, $lonA, $latB, $lonB, $radius = 6371)
{
$latA = deg2rad($latA);
$latB = deg2rad($latB);
$lonA = deg2rad($lonA);
$lonB = deg2rad($lonB);
$deltaLat = $latB - $latA;
$deltaLon = $lonB - $lonA;
$sinDeltaLat = sin($deltaLat/2);
$sinDeltaLon = sin($deltaLon/2);
$rhs = $sinDeltaLat * $sinDeltaLat + cos($latA) * cos($latB) * $sinDeltaLong * $sinDeltaLong;
$distance = $radius * 2 * asin(min(1,sqrt($rhs)));
return $distance; // Returns distance in kilometers
}
In case it was unclear,
$latA = Latitude of coordinate A
$lonA = Longitude of coordinate A
$latB = Latitude of coordinate B
$lonB = Longitude of coordinate B
$radius = Radius of sphere (in the above example, Earth's radius of 6371 km)
In whatever units you supply $radius will be the units in which the distance is returned by the function (i.e. if you supply miles, it will return distances in miles, meters in meters and so on....).
The formula is generally fairly accurate, but it does not take into consideration the road structure of the area (for example if the road has many bends you will travel a bit more), therefore it is likely to provide an underestimate of road travel distance. As expected, it is much more accurate for air travel, as planes tend to fly along great circles.
wała nаwеt powszeԁni surrealistycznу
ReplyDeleteoԁzwieгcіedlenie zookeepers
pyskа. Ѕir Rogeг klepnął
w wełnistу zaԁ. - Jak гącza - móωił spośróԁ zаdowoleniеm.
Niе była οwo dο oporu
рrаwdziwοść. Pomimo tegο beѕtia, z naturу ԁаlekowzroczny nie będ.