Skip to content

Instantly share code, notes, and snippets.

@gauravrai1
Created August 9, 2017 19:01
Show Gist options
  • Save gauravrai1/ed3d5a44b9149df5e59cb632c9ee9768 to your computer and use it in GitHub Desktop.
Save gauravrai1/ed3d5a44b9149df5e59cb632c9ee9768 to your computer and use it in GitHub Desktop.
haversini formula - PHP working
<?php
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371;
$distanceLat = deg2rad($latitude2 - $latitude1);
$distanceLon = deg2rad($longitude2 - $longitude1);
//part1 of the formula
$part1 = sin($distanceLat/2) * sin($distanceLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($distanceLon/2) * sin($distanceLon/2);
//part2 of the formula
$part2 = 2 * asin(sqrt($part1));
//part3 of the formula
$result = $earth_radius * $part2;
//return value
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment