0

Playing around with some of the browser API's around geolocation and device orientation.

If I know the persons orientation (think compass 0 - 360 degree) is it possible to know if they are pointing inline with a give latitude and longitude?

No idea on the maths or logic for this...

Lovelock
  • 7,689
  • 19
  • 86
  • 186

1 Answers1

0

Based on this answer, there is a formula for caluclating an angle from two pairs of coordinates (latitude and longitude):

const angleFromCoordinate = (lat1, lon1, lat2, lon2) => {
  // Returns angle in degrees based on coordinates
  const p1 = {
    x: lat1,
    y: lon1,
  };

  const p2 = {
    x: lat2,
    y: lon2,
  };
  return (Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180) / Math.PI;
};

You can use this function to get the direction (the angle) the destination is from current position and then compare it to the angle given by the compass, with accuracy depending on what you use it for.