可在Android项目中直接使用,建议建立LocationUtils类,直接调用static方法计算
- 输入:两点的经度、纬度
- 输出:两点间的方向角度
代码如下:
public static double bearing(double lat1, double lon1, double lat2, double lon2){
double longitude1 = lon1;
double longitude2 = lon2;
double latitude1 = Math.toRadians(lat1);
double latitude2 = Math.toRadians(lat2);
double longDiff = Math.toRadians(longitude2-longitude1);
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;
}