Created
April 17, 2017 13:24
-
-
Save vzool/a5b1e31fb45d93659c64cf94ba97837d to your computer and use it in GitHub Desktop.
Distance calculation from the latitude/longitude of 2 points
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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); | |
| } | |
| /* TESTS */ | |
| $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)"; | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment