Skip to content

Instantly share code, notes, and snippets.

@npostulart
Created November 18, 2018 19:39
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 npostulart/eb56e6d9a3f9e83dc48f6660e7ed31a3 to your computer and use it in GitHub Desktop.
Save npostulart/eb56e6d9a3f9e83dc48f6660e7ed31a3 to your computer and use it in GitHub Desktop.
Get heading from two lat/lng points
<?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