toRad() Javascript 函数抛出错误
我正在尝试使用 计算两个纬度经度点之间的距离? (半正矢公式)
代码如下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您缺少函数声明。
在 这种情况
toRad()
必须是首先定义为:根据页面底部
You are missing a function declaration.
In this case
toRad()
must be defined first as:according to the code segment all at the bottom of the page
或者就我而言,这不起作用。这可能是因为我需要在 jquery 中调用 toRad() 。我不是 100% 确定,所以我这样做了:
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:
我需要为我的项目计算点之间的大量距离,所以我继续尝试优化代码,我在这里找到了。平均而言,在不同的浏览器中,我的新实现运行速度几乎比此处提到的快 3 倍。
您可以使用我的 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.
You can play with my jsPerf (which was vastly improved thanks to Bart) and see the results here.
为什么不简化上面的方程并进行一些相同的计算呢?
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
我遇到了同样的问题..看着 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.
我改变了一些事情:
而且,我注意到没有检查
参数
。您应该确保定义了参数,并且可能在那里执行parseInt(arg, 10)
/parseFloat
。I changed a couple of things:
and, I noticed there was no checking for the
arguments
. You should make sure the args are defined AND probably do aparseInt(arg, 10)
/parseFloat
on there.