Skip to content

Instantly share code, notes, and snippets.

@jtwiest
Created February 22, 2017 20:15
Show Gist options
  • Save jtwiest/d37f55032ca240a049ca32c2c55dfd47 to your computer and use it in GitHub Desktop.
Save jtwiest/d37f55032ca240a049ca32c2c55dfd47 to your computer and use it in GitHub Desktop.
Return the distance between two points
/**
* Returns the distance between two given lat / lngs
* in miles.
*
* @params float $lat1
* @params float $lng1
* @params float $lat2
* @params float $lng2
* @return float
*/
public static function calculateDistance($lat1, $lng1, $lat2, $lng2)
{
$theta = $lng1 - $lng2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return $miles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment