你好,我有这个功能使用一个变量的函数外
locDistance(alat[i-1], alon[i-1], alat[i], alon[i]);
function locDistance(lat1, lon1, lat2, lon2) {
//Radius of the earth in: 1.609344 miles, 6371 km | var R = (6371/1.609344);
//var R = 3958.7558657440545;
var R = 3958.7558657440545;
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;
var dist = d;
};
function toRad(Value) {
/** Converts numeric degrees to radians */
return Value * Math.PI/180;
}
我知道我必须声明变量函数外,在另一个地方 ,所以我必须在
页面顶部定义为使用它var dist = 0;
我想在其他功能使用去变量 'DIST' 就像在这里
$('.content').append(dist);
但问题是我一直得到0作为dist变量的结果。
我检查了,但里面去locDistance功能DIST变量具有不同的值,因为它应该是这样的功能是不是这里的问题
我到底做错了什么?
预先感谢您
如果您至少使用相关编程语言进行了标记,这将有所帮助。 –
尽量避免使用全局变量,如果可以避免的话。在这种情况下,为什么不用'locaDistance'作为返回值返回距离? – Barmar
@Barmar说了什么......'return dist;'应该工作得很好...... – Shmiddty