【问题标题】:Calculate bearing between 2 points with javascript用javascript计算2点之间的方位
【发布时间】:2018-03-17 08:09:53
【问题描述】:

我想计算从点 1 到点 2 的方位角 输入格式为 52.070564 - 4.407116

无论我尝试什么,我都无法获得正确的输出。

我使用的公式是:

// 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)
*/

【问题讨论】:

  • 你能给我们一些示例输入和示例输出吗?
  • Math.sin()Math.cos() 希望他们的输入以弧度为单位。看起来你的输入是度数,所以你需要转换它们。
  • @Rogier 检查我的解决方案
  • 52.07129-4.4056 和 52.07091-4.4048 的方位角应该在 230* 左右

标签: javascript bearing


【解决方案1】:

您可以使用以下函数计算方位:

// 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;
}

【讨论】:

    【解决方案2】:

    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);
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 清除,所以我的输入需要以弧度表示?我将尝试找到如何转换它。谢谢!
    • 我试过了,但仍然不是预期的结果。我使用 (* Math.PI / 180) 将输入从度数更改为半径。当我拿 2 点时,一个是我的花园,另一个是在街道的另一边,我得到 20 的方位。但是当我在我的 iPhone 上拿到指南针时,那个点的方位是 250。我弄错了吗在思考???
    • 非常适合我使用带有实时移动对象的传单地图。我使用 css 转换从结果中创建了一个指南针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2021-08-06
    • 1970-01-01
    • 2011-04-18
    • 2013-05-21
    相关资源
    最近更新 更多