2012-02-26 133 views
15

我实现了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; 
} 

回答

13

你只是有你的括号()在错误的地方。

您正在向以弧度表示的值添加度数,这不起作用。 toDegrees()将为你做从弧度到度的转换,然后你做了规范化,一旦你有一个度数值。

您有:

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

但是,你需要:

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

也请记住,所有输入Math.sin()Math.cos()和所有其他的三角函数必须弧度。如果您的输入是度数,则需要先使用Math.toRadians()进行转换。

+0

权!但仍然输入参数是 ProcJobLocation.bearing(53.944592,27.595215,55.745752,37.630768); 和输出是359.11592632310266。 仍然有一些错误。 – 2012-02-26 23:03:44

+1

您的输入似乎是度数。你需要使用'Math.toRadians()'将它们转换为弧度,否则'Math.sin()','Math.cos()'等会给出错误的结果。 – DNA 2012-02-26 23:07:00

+0

谢谢你的帮助! – 2012-02-27 09:33:14

43

下面是最终代码:

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

其中lat1,lon1,lat2和lon2以弧度表示!可能不会。那么为什么你重命名经度1和经度2? – Radu 2016-12-12 14:13:01

8

从一个轴承坐标到另一个,找到北,东,南,weast :) 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 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; 
    } 
} 
相关问题