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.