Created
June 8, 2017 02:40
-
-
Save ivmelo/dd69b4cf3a28a1eabc0c13c5a099da91 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Calculates the great-circle distance between two points | |
* using the Haversine formula. | |
* | |
* @param float $latitudeFrom Latitude of start point in [deg decimal] | |
* @param float $longitudeFrom Longitude of start point in [deg decimal] | |
* @param float $latitudeTo Latitude of target point in [deg decimal] | |
* @param float $longitudeTo Longitude of target point in [deg decimal] | |
* @param float $earthRadius Mean earth radius in [m] | |
* | |
* @return float Distance between points in [m] (same as earthRadius) | |
**/ | |
private function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) | |
{ | |
// Convert from degrees to radians. | |
$latFrom = deg2rad($latitudeFrom); | |
$lonFrom = deg2rad($longitudeFrom); | |
$latTo = deg2rad($latitudeTo); | |
$lonTo = deg2rad($longitudeTo); | |
$latDelta = $latTo - $latFrom; | |
$lonDelta = $lonTo - $lonFrom; | |
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) + | |
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2))); | |
return $angle * $earthRadius; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment