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 *a ^{2} + b^{2} = c^{2}* 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 (check the haversine formula article on Wikipedia for more details). Rather, let me just give you a very general description of the function:

- Using the Haversine formula, 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 the haversine formula.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php /* Description: Distance calculation from the latitude/longitude of 2 points Author: Rajesh Singh (2014) Website: http://AssemblySys.com If you find this script useful, you can show your appreciation by getting Rajesh a cup of coffee ;) PayPal: rajesh.singh@assemblysys.com As long as this notice (including author name and details) is included and UNALTERED, this code is licensed under the GNU General Public License version 3: http://www.gnu.org/licenses/gpl.html */ 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, taking coordinates from our worldCities database.

1 2 3 4 5 6 7 8 |
<?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 other formulas are mentioned, some more accurate for long distances and others for short distances.

Joshua SantiagoGreat formula. It works like a charm

Rajesh SinghPost authorThank you very much!

mahendra prataphelpful article thanks

rubyat islamThanks a lot

MichaelYou’re welcome! 🙂