Created
November 18, 2018 19:39
-
-
Save npostulart/eb56e6d9a3f9e83dc48f6660e7ed31a3 to your computer and use it in GitHub Desktop.
Get heading from two lat/lng points
This file contains hidden or 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 | |
function computeHeading($from, $to) { | |
$fromLat = deg2rad($from['lat']); | |
$fromLng = deg2rad($from['lng']); | |
$toLat = deg2rad($to['lat']); | |
$toLng = deg2rad($to['lng']); | |
$dLng = $toLng - $fromLng; | |
$heading = rad2deg( | |
atan2( | |
sin($dLng) * cos($toLat), | |
cos($fromLat) * sin($toLat) - sin($fromLat) * cos($toLat) * cos($dLng) | |
) | |
); | |
$min = -180; | |
$max = 180; | |
if ($heading >= $min && $heading < $max) { | |
return $heading; | |
} | |
$x = $heading - $min; | |
$m = $max - $min; | |
return ((($x % $m) + $m) % $m) + $min; | |
} | |
$from = ['lat' => 52.027254, 'lng' => 8.539802]; | |
$to = ['lat' => 52.024667, 'lng' => 8.539887]; | |
$heading = computeHeading($from, $to); | |
echo $heading; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment