Skip to content

Instantly share code, notes, and snippets.

@ivmelo
Created June 8, 2017 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivmelo/dd69b4cf3a28a1eabc0c13c5a099da91 to your computer and use it in GitHub Desktop.
Save ivmelo/dd69b4cf3a28a1eabc0c13c5a099da91 to your computer and use it in GitHub Desktop.
<?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