Calculate bearing between 2 points with javascript
Asked Answered
W

2

9

I want to calculate the bearing from point 1 to point 2 The input format is 52.070564 - 4.407116

No matter what i try i cannot get an correct output.

The formula i use is :

// koers berekenen
var y = Math.sin(ln-ln1) * Math.cos(lt);
var x = Math.cos(lt1)*Math.sin(lt) -
        Math.sin(lt1)*Math.cos(lt)*Math.cos(ln-ln1);
var radians = Math.atan2(y, x);
var bearing = Math.round(radians * (180/Math.PI));

/* >>>>>> original <<<<<<<
var y = Math.sin(λ2-λ1) * Math.cos(φ2);
var x = Math.cos(φ1)*Math.sin(φ2) -
        Math.sin(φ1)*Math.cos(φ2)*Math.cos(λ2-λ1);
var brng = Math.atan2(y, x).toDegrees();

φ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude)
*/
Walk answered 5/10, 2017 at 16:3 Comment(5)
Can you give us some example inputs and example outputs?Monkshood
Math.sin() and Math.cos() expect their inputs to be in radians. It looks like your inputs are degrees, so you need to convert them.Itchy
@Walk check my solutionGaliot
52.07129-4.4056 and 52.07091-4.4048 should give a bearing around 230*Walk
Your snippet produces: "message": "Uncaught ReferenceError: ln is not defined", "filename": "stacksnippets.net/js",Aorta
C
34

You can calculate bearing using this functions:

// Converts from degrees to radians.
function toRadians(degrees) {
  return degrees * Math.PI / 180;
};
 
// Converts from radians to degrees.
function toDegrees(radians) {
  return radians * 180 / Math.PI;
}


function bearing(startLat, startLng, destLat, destLng){
  startLat = toRadians(startLat);
  startLng = toRadians(startLng);
  destLat = toRadians(destLat);
  destLng = toRadians(destLng);

  y = Math.sin(destLng - startLng) * Math.cos(destLat);
  x = Math.cos(startLat) * Math.sin(destLat) -
        Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
  brng = Math.atan2(y, x);
  brng = toDegrees(brng);
  return (brng + 360) % 360;
}
Cassandry answered 29/8, 2018 at 13:58 Comment(0)
G
6

start_latitude  = 12.9389352
start_longitude = 77.6994306
stop_latitude   = 12.939103
stop_longitude  = 77.705825

var y = Math.sin(stop_longitude-start_longitude) * Math.cos(stop_latitude);
var x = Math.cos(start_latitude)*Math.sin(stop_latitude) -
        Math.sin(start_latitude)*Math.cos(stop_latitude)*Math.cos(stop_longitude-start_longitude);
var brng = Math.atan2(y, x) * 180 / Math.PI;


alert("Bearing in degreee:  " + brng);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Galiot answered 5/10, 2017 at 16:32 Comment(3)
Clear, so my input needs to be in radians? I will try to find how to convert this. Thanks!Walk
I tried but still not the expected result. I change the input from degrees to radius with (* Math.PI / 180). When i take 2 points, one is my garden and the other is on the other side of the street i get a bearing of 20. But when i get the compass on my iphone the bearing to that point is 250. Am i making a mistake in thinking???Walk
works great for me using leaflet maps with and real-time moving objects. ive created a compass out of the result using css transforms.Shit

© 2022 - 2024 — McMap. All rights reserved.