正如标题所说,我有当前位置,并且想知道从我当前位置到其他位置的度数。我已经阅读了这篇文章,location.getBearing() 返回的值是我的答案吗?
简单说一下:从图片上看,我期待的值是45度。
我在执行此操作时遇到了麻烦,并设法弄清楚如何转换 C?轴承度数的计算方法。
我使用 4 个坐标点解决了这个问题
开始纬度
起始经度
结束纬度
结束经度
这是代码:
protected double bearing(double startLat, double startLng, double endLat, double endLng){
double latitude1 = Math.toRadians(startLat);
double latitude2 = Math.toRadians(endLat);
double longDiff= Math.toRadians(endLng - startLng);
double y= Math.sin(longDiff)*Math.cos(latitude2);
double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
return (Math.toDegrees(Math.atan2(y, x))+360)%360;
}
只需更改需要的变量名称。
将方位输出到 TextView
希望它有所帮助!
位置有方法bearingTo:浮动bearingTo(Location dest)
Location from = new Location(LocationManager.GPS_PROVIDER);
Location to = new Location(LocationManager.GPS_PROVIDER);
from.setLatitude(0);
from.setLongitude(0);
to.setLongitude(10);
to.setLatitude(10);
float bearingTo = from.bearingTo(to);
这是计算两点之间的方位角的代码(startPoint,endPoint):
public float CalculateBearingAngle(double startLatitude,double startLongitude, double endLatitude, double endLongitude){
double Phi1 = Math.toRadians(startLatitude);
double Phi2 = Math.toRadians(endLatitude);
double DeltaLambda = Math.toRadians(endLongitude - startLongitude);
double Theta = atan2((sin(DeltaLambda)*cos(Phi2)) , (cos(Phi1)*sin(Phi2) - sin(Phi1)*cos(Phi2)*cos(DeltaLambda)));
return (float)Math.toDegrees(Theta);
}
调用函数:
float angle = CalculateBearingAngle(startLatitude, startLongitude, endLatitude, endLongitude);