【问题标题】:toRad() Javascript function throwing errortoRad() Javascript 函数抛出错误
【发布时间】:2023-03-12 22:22:01
【问题描述】:

我正在尝试使用Calculate distance between two latitude-longitude points? (Haversine formula)@ 中描述的技术来查找两点之间的距离(我有纬度和经度)

代码如下 Javascript:

var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad();  // Javascript functions in radians
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c; // Distance in km

但是当我尝试实现它时,会出现错误提示Uncaught TypeError: Object 20 has no Method 'toRad'

我是否需要一个特殊的库或其他东西才能让 .toRad() 工作?因为它似乎是 搞砸了第二行。

【问题讨论】:

  • 请参阅my answer 优化版本的Haversine 距离。平均而言,它的运行速度是您启动解决方案的两倍。

标签: javascript geolocation


【解决方案1】:

您缺少函数声明。

this casetoRad()必须先定义为:

/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

根据代码段全部在bottom of the page

【讨论】:

    【解决方案2】:

    或者在我的情况下这不起作用。这可能是因为我需要在 jquery 中调用 toRad()。我不是 100% 确定,所以我这样做了:

    function CalcDistanceBetween(lat1, lon1, lat2, lon2) {
        //Radius of the earth in:  1.609344 miles,  6371 km  | var R = (6371 / 1.609344);
        var R = 3958.7558657440545; // Radius of earth in Miles 
        var dLat = toRad(lat2-lat1);
        var dLon = toRad(lon2-lon1); 
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
                Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var d = R * c;
        return d;
    }
    
    function toRad(Value) {
        /** Converts numeric degrees to radians */
        return Value * Math.PI / 180;
    }
    

    【讨论】:

      【解决方案3】:

      我需要为我的项目计算很多点之间的距离,所以我继续尝试优化代码,我在这里找到了。平均而言,在不同的浏览器中,我的新实现运行速度几乎比这里提到的快 3 倍

      function distance(lat1, lon1, lat2, lon2) {
        var R = 6371; // Radius of the earth in km
        var dLat = (lat2 - lat1) * Math.PI / 180;  // deg2rad below
        var dLon = (lon2 - lon1) * Math.PI / 180;
        var a = 
           0.5 - Math.cos(dLat)/2 + 
           Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * 
           (1 - Math.cos(dLon))/2;
      
        return R * 2 * Math.asin(Math.sqrt(a));
      }
      

      您可以使用我的 jsPerf(感谢 Bart 大大改进)并查看 results here

      【讨论】:

        【解决方案4】:

        为什么不简化上面的等式并做同样的一些计算呢?

        Math.sin(dLat/2) * Math.sin(dLat/2) = (1.0-Math.cos(dLat))/2.0

        Math.sin(dLon/2) * Math.sin(dLon/2) = (1.0-Math.cos(dLon))/2.0

        【讨论】:

        • 后期添加:因为三角恒等式是抽象的;对于小距离,cos 版本的数值条件较差。此外,优化编译器可能会排除常用术语。微优化不佳。
        【解决方案5】:

        我遇到了同样的问题.. 查看 Casper 的答案,我只是做了一个快速修复:Ctrl+H(查找和替换), 替换了.toRad() 的所有实例 与* Math.PI / 180。这对我有用。

        不过,不知道浏览器的性能速度等。我的用例仅在用户单击地图时才需要。

        【讨论】:

          【解决方案6】:

          我改变了几件事:

          if (!Number.prototype.toRad || (typeof(Number.prototype.toRad) === undefined)) {
          

          而且,我注意到没有检查 arguments。您应该确保 args 已定义并且可能在那里执行 parseInt(arg, 10) / parseFloat

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-11-25
            • 1970-01-01
            • 1970-01-01
            • 2021-09-30
            • 1970-01-01
            • 2012-02-21
            • 2020-03-01
            • 1970-01-01
            相关资源
            最近更新 更多