# Haversine Formula

Discussion in 'PHP' started by pradeep, May 30, 2005.

Joined:
Apr 4, 2005
Messages:
1,645
87
Trophy Points:
0
Occupation:
Programmer
Location:
Kolkata, India
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:
``` # 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 (1, 0.707106781186548 * sqrt ((1 - (sin (\$l2) * \$sinl1) - cos (\$l1) * cos (\$l2) * cos (\$o2 - \$o1))))); } ```