PHP: How to get aerial distance between two GPS coordinates

I wanted to calculate the distance between two GPS coordinates in Kilometres
There’s an algorithm called “Haversine Great Circle Distance” that does just that.
Here’s a PHP implementation of the Great Circle Distance algorithm:

function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);

// calculate delta
$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 * 6371; // earth radius in km
}

Author: nurnachman

Software Developer, Love Cats.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.