toRad() Javascript 函数抛出错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5260423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
toRad() Javascript function throwing error
提问by ptamzz
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)
我正在尝试使用计算两个纬度 - 经度点之间的距离中描述的技术来找到两点之间的距离(我有纬度和经度)?(Haversine 公式)
The codes are as below 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
But when I try to implement it, an error shows up saying Uncaught TypeError: Object 20 has no Method 'toRad'
.
但是当我尝试实现它时,会出现一个错误,说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.
我需要一个特殊的库或其他东西来让 .toRad() 工作吗?因为它似乎在第二行搞砸了。
回答by Caspar Kleijne
You are missing a function declaration.
您缺少函数声明。
In this casetoRad()
must be defined first as:
在这种情况下,toRad()
必须首先定义为:
/** 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
根据页面底部的代码段
回答by Salvador Dali
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 fasterthan mentioned here.
我需要为我的项目计算点之间的很多距离,所以我继续尝试优化代码,我在这里找到了。平均而言,我的新实现在不同浏览器中的运行速度几乎比这里提到的快 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));
}
You can play with my jsPerf (which was vastly improved thanks to Bart) and see the results here.
回答by Christopher Gray
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:
或者在我的情况下这不起作用。这可能是因为我需要在 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;
}
回答by jostmey
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(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
Math.sin(dLon/2) * Math.sin(dLon/2) = (1.0-Math.cos(dLon))/2.0
回答by Nikhil VJ
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.
我遇到了同样的问题..看着 Casper 的回答,我只是做了一个快速修复:(Ctrl+H
查找和替换),.toRad()
用* Math.PI / 180
. 那对我有用。
No idea on the browser performance speeds etc, though.. My use case only needs this when the user clicks on a map.
但是,不知道浏览器的性能速度等。我的用例只在用户点击地图时才需要这个。
回答by Cody
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.
而且,我注意到没有检查arguments
. 你应该确保定义了 args 并且可能在那里做一个parseInt(arg, 10)
/ parseFloat
。