【问题标题】:Bearing from one coordinate to another从一个坐标到另一个坐标的方位角
【发布时间】:2012-03-16 12:15:47
【问题描述】:

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

【问题讨论】:

    标签: java geolocation bearing


    【解决方案1】:

    这是最终代码:

    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 呢??
    【解决方案2】:

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

    您正在为弧度值添加度数,这不起作用。 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。还是有一些错误。
    • 您的输入似乎是度数。您需要使用Math.toRadians() 将它们转换为弧度,否则Math.sin()Math.cos() 等会给出错误的结果。
    【解决方案3】:

    从一个坐标到另一个坐标的方位并找到北、东、南、西:)

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

    【讨论】:

      【解决方案4】:

      @IvanT answer 的一点清理版本:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 1970-01-01
        相关资源
        最近更新 更多