toRad() Javascript 函数抛出错误

发布于 2024-10-21 16:57:11 字数 724 浏览 1 评论 0原文

我正在尝试使用 计算两个纬度经度点之间的距离? (半正矢公式)

代码如下 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() 工作?因为这似乎是 把第二行搞砸了。

I'm trying to find the distance between two points (for which I've latitudes & longitudes) using the technique described here at Calculate distance between two latitude-longitude points? (Haversine formula)

The codes are as below
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

But when I try to implement it, an error shows up saying Uncaught TypeError: Object 20 has no Method 'toRad'.

Do I need a special library or something to get .toRad() working? because it seems to be
screwing up on the second line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

面如桃花 2024-10-28 16:57:11

您缺少函数声明。

这种情况 toRad() 必须是首先定义为:

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

根据页面底部

You are missing a function declaration.

In this case toRad() must be defined first as:

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

according to the code segment all at the bottom of the page

夜空下最亮的亮点 2024-10-28 16:57:11

或者就我而言,这不起作用。这可能是因为我需要在 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;
}

Or in my case this didn't work. It may because i needed to call toRad() inside jquery. Im not 100% sure, so i did this:

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;
}
世俗缘 2024-10-28 16:57:11

我需要为我的项目计算点之间的大量距离,所以我继续尝试优化代码,我在这里找到了。平均而言,在不同的浏览器中,我的新实现运行速度几乎比此处提到的快 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,它得到了极大的改进)并在此处查看结果

I needed to calculate a lot of distances between the points for my project, so I went ahead and tried to optimize the code, I have found here. On average in different browsers my new implementation runs almost 3 times faster than mentioned here.

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

You can play with my jsPerf (which was vastly improved thanks to Bart) and see the results here.

你对谁都笑 2024-10-28 16:57:11

为什么不简化上面的方程并进行一些相同的计算呢?

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

Why not simplify the above equation and same a few computations?

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

ゝ杯具 2024-10-28 16:57:11

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

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

I was having the same problem.. looking at Casper's answer, I just did a quick-fix: Ctrl+H (Find and Replace),
replaced all instances of .toRad()
with * Math.PI / 180 . That worked for me.

No idea on the browser performance speeds etc, though.. My use case only needs this when the user clicks on a map.

终止放荡 2024-10-28 16:57:11

我改变了一些事情:

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

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

I changed a couple of things:

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

and, I noticed there was no checking for the arguments. You should make sure the args are defined AND probably do a parseInt(arg, 10) / parseFloat on there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文