Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Haversine Formula (http://www.go4expert.com/articles/haversine-formula-t339/)

pradeep 30May2005 18:13

Haversine Formula
Haversine Formula

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical "triangles".

(The formulas could equally be written in terms of any multiple of the haversine, such as the older versine function (twice the haversine). Historically, the haversine had, perhaps, a slight advantage in that its maximum is one, so that logarithmic tables of its values could end at zero. These days, the haversine form is also convenient in that it has no coefficient in front of the sin2 function.)

PHP Code:

# Where:
#  $l1 ==> latitude1
#  $o1 ==> longitude1
#  $l2 ==> latitude2
#  $o2 ==> longitude2
function haversine ($l1$o1$l2$o2)
$l1 deg2rad ($l1);
$sinl1 sin ($l1);
$l2 deg2rad ($l2);
$o1 deg2rad ($o1);
$o2 deg2rad ($o2);
    return (
7926 26 $sinl1) * asin (min (10.707106781186548 sqrt ((- (sin ($l2) * $sinl1) - cos ($l1) * cos ($l2) * cos ($o2 $o1)))));

Further reading:

All times are GMT +5.5. The time now is 21:11.