在我的 webapp 中,有一个来自数据库查询的 JSON 数据响应,其中包括 1 到 n 个位置的纬度/经度坐标。我想计算从该data[i]
位置到当前位置的方位。
我一直在修改这里的代码,但是返回的方位不正确。
//starting lat/long along with converting lat to rads
var endLat = toRad(location.lat());
var endLong = location.lng();
//loop over response, calculate new headings for links and add link to array
for(var i=0; i<data.length; i++){
//this link's lat/long coordinates, convert lat to rads
var startLat = toRad(data[i].lat);
var startLong = data[i].lon;
//get the delta values between start and end coordinates in rads
var dLong = toRad(endLong - startLong);
//calculate
var y = Math.sin(dLong)*Math.cos(endLong);
var x = Math.cos(startLat)*Math.sin(endLat)-Math.sin(startLat)*Math.cos(endLat)*Math.cos(dLong);
var bearing = Math.atan(y, x);
bearing = (toDeg(bearing) + 360) % 360;
panoLinks.push({'heading': bearing, 'description': data[i].description, 'pano': data[i].description});
}
//radian/degree conversions
function toRad(convert){
return convert * Math.PI/180;
}
function toDeg(convert){
return convert * 180/Math.PI;
}
使用上面的函数和值
startLat= 43.6822, converts to 0.7623982145146669 radians
startLong= -70.450769
endLat= 43.682211, converts to 0.7623984065008848 radians
endLong= -70.45070
dLong = startLong - endLong, converts to 0.0000011170107216805305 radians
导致罗盘度数
bearing= 0.000014910023935499339
这绝对是关闭的。我哪里出错了?