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

19

我实现了来自http://www.movable-type.co.uk/scripts/latlong.html的"方位"公式,但它似乎非常不准确 - 我怀疑我的实现中存在一些错误。你可以帮我找出问题吗?我的代码如下:

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;
}
4个回答

60

这是最终的代码:

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;
}

lat1、lon1、lat2 和 lon2 是否以弧度为单位?!可能不是。那么为什么要重命名 longitude1 和 longitude2 呢? - Radu

19
你只是把括号()放错了位置。
你正在将度数加到弧度值上,这样是行不通的。toDegrees()会为您从弧度转换为度数,然后一旦您获得度数值,就进行归一化处理。
你有:
 Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;

但是你需要:

( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;
记住,所有传递给 Math.sin()Math.cos() 和其他三角函数的输入都必须以弧度为单位。如果你的输入是度数,你需要先通过 Math.toRadians() 进行转换。

没错!但是输入参数仍然是ProcJobLocation.bearing(53.944592, 27.595215, 55.745752, 37.630768);,输出结果为359.11592632310266。仍然存在一些错误。 - Ivan T
1
您的输入似乎是以角度为单位的。 您需要使用Math.toRadians()将它们转换为弧度,否则Math.sin()Math.cos()等函数将会给出错误的结果。 - DNA

11
从一个坐标到另一个坐标的方位和找到北、东、南、西 :)enter image description here
     public class FindBearing {
            public static void main(String[] args) {
                System.out.println(" Your Result >>> "+FindBearing.bearing(19.2859590, 73.4966430, 19.2861020, 73.4988090));    
            }   
            protected static String 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);
          double resultDegree= (Math.toDegrees(Math.atan2(y, x))+360)%360;
          String coordNames[] = {"N","NNE", "NE","ENE","E", "ESE","SE","SSE", "S","SSW", "SW","WSW", "W","WNW", "NW","NNW", "N"};
          double directionid = Math.round(resultDegree / 22.5); 
          // no of array contain 360/16=22.5
          if (directionid < 0) {
              directionid = directionid + 16;
               //no. of contains in array
          }
          String compasLoc=coordNames[(int) directionid];

          return resultDegree+" "+compasLoc;
        }
            }

2

以下是稍微改进的 @IvanT 的回答:

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);
}

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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接