已知两点经纬度,计算偏移角(正北为0,顺时针为正,逆时针为负)
程序员文章站
2022-06-15 23:36:51
function getAngle(preCoord,currCoord){var ret var w1 = preCoord.lat/180 * Math.PI var j1 = preCoord.lng/180 * Math.PI var w2 = currCoord.lat/180 * Math.PI var j2 = currCoord.lng/180 * Math.PI ret = 4 * Math.pow(Math.sin((w1 - w2...
function getAngle(preCoord,currCoord){
var ret
var w1 = preCoord.lat/180 * Math.PI
var j1 = preCoord.lng/180 * Math.PI
var w2 = currCoord.lat/180 * Math.PI
var j2 = currCoord.lng/180 * Math.PI
ret = 4 * Math.pow(Math.sin((w1 - w2) / 2), 2) - Math.pow(Math.sin((j1 - j2) / 2) * (Math.cos(w1) - Math.cos(w2)), 2);
ret = Math.sqrt(ret);
// var temp = Math.sin(Math.abs(j1 - j2) / 2) * (Math.cos(w1) + Math.cos(w2));
var temp = Math.sin((j1 - j2) / 2) * (Math.cos(w1) + Math.cos(w2));
// console.log(temp)
ret = ret/temp;
ret = Math.atan(ret) / Math.PI * 180 ;
ret += 90;
// 这里用如此臃肿的if..else是为了判定追踪单个点的具体情况,从而调整ret的值
if(j1-j2 < 0){
// console.log('j1<j2')
if(w1-w2 < 0){
// console.log('w1<w2')
ret;
}else{
// console.log('w1>w2')
ret = -ret+180;
}
}else{
// console.log('j1>j2')
if(w1-w2 < 0){
// console.log('w1<w2')
ret = 180+ret;
}else{
// console.log('w1>w2')
ret = -ret;
}
}
return ret ;
}
本文地址:https://blog.csdn.net/weixin_43422861/article/details/107547804