Geographical distance calculation is a common cause of headaches. Indeed, if the earth were flat, you could simply use Pythagoras’ theorem, which states that in a right triangle, the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides (you probably remember its formula a2 + b2 = c2 from high school). But of course, the Earth is not flat…
In the following distance calculation function, I use the haversine formula to calculate the distance between two points on the Earth’s surface, feeding it the latitude and longitude of both points.
The calculation is a bit tricky, so I won’t describe it in details. Rather, let me just give you a very general description of the function:
- First, we calculate the distance in degrees between the two sets of coordinates
- The value in degrees is converted to kilometers (by default), miles or nautical miles, depending on your needs.
- The distance is returned, with the chosen precision (2 decimals by default).
Keep in mind that since the Earth is not a perfect sphere, there is a little margin of error associated to this formula.
<?php /* Description: Distance calculation from the latitude/longitude of 2 points Author: Michaël Niessen (2014) Website: http://AssemblySys.com If you find this script useful, you can show your appreciation by getting Michaël a cup of coffee ;) https://ko-fi.com/assemblysys As long as this notice (including author name and details) is included and UNALTERED, this code can be freely used and distributed. */ function distanceCalculation($point1_lat, $point1_long, $point2_lat, $point2_long, $unit = 'km', $decimals = 2) { // Calculate the distance in degrees $degrees = rad2deg(acos((sin(deg2rad($point1_lat))*sin(deg2rad($point2_lat))) + (cos(deg2rad($point1_lat))*cos(deg2rad($point2_lat))*cos(deg2rad($point1_long-$point2_long))))); // Convert the distance in degrees to the chosen unit (kilometres, miles or nautical miles) switch($unit) { case 'km': $distance = $degrees * 111.13384; // 1 degree = 111.13384 km, based on the average diameter of the Earth (12,735 km) break; case 'mi': $distance = $degrees * 69.05482; // 1 degree = 69.05482 miles, based on the average diameter of the Earth (7,913.1 miles) break; case 'nmi': $distance = $degrees * 59.97662; // 1 degree = 59.97662 nautic miles, based on the average diameter of the Earth (6,876.3 nautical miles) } return round($distance, $decimals); } ?>
Here is an example of how to use that function:
<?php $point1 = array("lat" => "48.8666667", "long" => "2.3333333"); // Paris (France) $point2 = array("lat" => "19.4341667", "long" => "-99.1386111"); // Mexico City (Mexico) $km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); // Calculate distance in kilometres (default) $mi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'mi'); // Calculate distance in miles $nmi = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long'], 'nmi'); // Calculate distance in nautical miles echo "The distance between Paris (France) and Mexico City (Mexico) is $km km (= $mi miles = $nmi nautical miles)"; ?>
For more details about the difficulties of distance calculation, check the Wikipedia article on geographical distance, in which a few formulas are mentioned, some more accurate for long distances and others for short distances.