Java 从一个坐标到另一个坐标的方位角

Java 从一个坐标到另一个坐标的方位角,java,geolocation,bearing,Java,Geolocation,Bearing,我从中实现了“轴承”公式。但它似乎非常不准确——我怀疑我的实现中存在一些错误。你能帮我找到它吗?我的代码如下: protected static double bearing(double lat1, double lon1, double lat2, double lon2){ double longDiff= lon2-lon1; double y = Math.sin(longDiff)*Math.cos(lat2); double x = Math.cos(lat1)*Math.sin

我从中实现了“轴承”公式。但它似乎非常不准确——我怀疑我的实现中存在一些错误。你能帮我找到它吗?我的代码如下:

protected static double bearing(double lat1, double lon1, double lat2, double lon2){

double longDiff= lon2-lon1;
double y = Math.sin(longDiff)*Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(longDiff);

return Math.toDegrees((Math.atan2(y, x))+360)%360;
}

你只是把括号
()
放错地方了

您正在将度数添加到弧度值,这将不起作用
toDegrees()
将为您执行从弧度到度的转换,然后在获得度值后执行归一化

你有:

 Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;
但你需要:

( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;

还要记住,
Math.sin()
Math.cos()
和所有其他三角函数的所有输入必须以弧度为单位。如果您的输入是度,则需要首先使用
Math.toRadians()
转换它们。

以下是最终代码:

protected 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;
}
从一个坐标指向另一个坐标,并找到北、东、南、西:)

公共类FindBearing{
公共静态void main(字符串[]args){
System.out.println(“您的结果>>>”+FindBearing.bearing.bearing(19.2859590973.496643019.286102073.4988090));
}   
受保护静态串轴承(双lat1、双lon1、双lat2、双lon2){
双纵1=1;
双纵2=lon2;
双纬度1=数学环面(纬度1);
双纬度2=数学环面(纬度2);
double longDiff=数学环面(longitude2-longitude1);
双y=数学sin(longDiff)*数学cos(latitude2);
double x=Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff);
双结果度=(数学到度(数学atan2(y,x))+360)%360;
字符串坐标名称[]={“N”、“NNE”、“NE”、“ENE”、“E”、“ESE”、“SE”、“SSE”、“S”、“SSW”、“SW”、“WSW”、“W”、“WNW”、“NW”、“NNW”、“N”};
双向ID=数学圆(结果度/22.5);
//包含360/16的阵列数量=22.5
如果(方向ID<0){
方向ID=方向ID+16;
//数组中的包含数
}
字符串compasLoc=coordNames[(int)directionid];
返回结果degree+“”+compasLoc;
}
}

稍微清理一下以下版本:

其中
LatLng
为:

public final class LatLng {
    private final double latitude;
    private final double longitude;

    public LatLng(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

正确的!但仍然输入参数是ProcJobLocation.bearing(53.944592,27.595215,55.745752,37.630768);输出为359.11592632310266。仍然有一些错误。你的输入似乎是以度为单位的。您需要使用
Math.toRadians()
将其转换为弧度,否则
Math.sin()
Math.cos()
等将给出错误的结果。lat1、lon1、lat2和lon2的弧度在哪里???!可能不会。那么为什么要重命名longitude1和longitude2??
public static double bearingInRadians(LatLng src, LatLng dst) {
    double srcLat = Math.toRadians(src.getLatitude());
    double dstLat = Math.toRadians(dst.getLatitude());
    double dLng = Math.toRadians(dst.getLongitude() - src.getLongitude());

    return Math.atan2(Math.sin(dLng) * Math.cos(dstLat),
            Math.cos(srcLat) * Math.sin(dstLat) - 
              Math.sin(srcLat) * Math.cos(dstLat) * Math.cos(dLng));
}

public static double bearingInDegrees(LatLng src, LatLng dst) {
    return Math.toDegrees((bearingInRadians(src, dst) + Math.PI) % Math.PI);
}
public final class LatLng {
    private final double latitude;
    private final double longitude;

    public LatLng(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}