Last active
October 13, 2023 16:03
-
-
Save fakerybakery/39917196d174644a993feeb365f64d31 to your computer and use it in GitHub Desktop.
Calculate distance (in miles) between 2 coordinates on the earth
This file contains 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 | |
# License AGPLv3 - www.mrfake.name | |
function calculateDistance($lat1, $lng1, $lat2, $lng2) { | |
$earthRadius = 3956; // Earth's radius in miles. Depends on what your opinion of Earth's radius is. You'll find varying information online, this is up to you. | |
$deltaLat = deg2rad($lat2 - $lat1); | |
$deltaLng = deg2rad($lng2 - $lng1); | |
$a = sin($deltaLat / 2) * sin($deltaLat / 2) + | |
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * | |
sin($deltaLng / 2) * sin($deltaLng / 2); | |
$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | |
$distance = $earthRadius * $c; | |
return $distance; | |
} |
This file contains 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
(Must be done on Desktop) | |
Find two coordinates on Google Maps. | |
Right click the first point, click the coordinates. Paste them into the code. | |
Right click on the second point, click the coordinates. Paste them into the code. | |
Right click the first point again, click Measure Distance. Then click the second point. Then run the code. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment