2012-02-26 69 views
15

Tôi đã triển khai công thức "mang" từ http://www.movable-type.co.uk/scripts/latlong.html. Nhưng nó có vẻ rất không chính xác - tôi nghi ngờ một số sai lầm trong việc thực hiện của tôi. Bạn có thể giúp tôi tìm nó không? Mã của tôi bên dưới:Mang từ một tọa độ này sang một tọa độ khác

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

Trả lời

13

Bạn chỉ có dấu ngoặc đơn () ở vị trí sai.

Bạn đang thêm độ vào giá trị theo radian, điều này sẽ không hoạt động. toDegrees() sẽ thực hiện chuyển đổi từ radian thành độ cho bạn, rồi bạn thực hiện bình thường hóa khi bạn có giá trị bằng độ.

Bạn có:

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

Nhưng bạn cần:

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

cũng nhớ rằng tất cả nguyên liệu đầu vào để Math.sin(), Math.cos() và tất cả các hàm lượng giác khác phải theo radian. Nếu đầu vào của bạn là độ bạn sẽ cần phải chuyển đổi chúng bằng cách sử dụng Math.toRadians() trước tiên.

+0

Phải! Nhưng các thông số đầu vào vẫn là ProcJobLocation.bearing (53.944592, 27.595215, 55.745752, 37.630768); và đầu ra là 359.11592632310266. Vẫn còn một số sai lầm. –

+1

Các yếu tố đầu vào của bạn dường như bằng độ. Bạn cần chuyển đổi chúng thành radian bằng 'Math.toRadians()', nếu không 'Math.sin()', 'Math.cos()' vv sẽ cho kết quả sai. – DNA

+0

Cảm ơn sự giúp đỡ của bạn! –

43

Đây là mã cuối cùng:

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

nơi lat1, lon1, lat2 và lon2 đang ở radian ???! Chắc là không. vậy tại sao bạn đổi tên longitude1 và longitude2 ?? – Radu

8

Bạc đạn từ một phối hợp khác Và Tìm Bắc, Đông, Nam, 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

Một chút làm sạch phiên bản của @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); 
} 

Nơi LatLng là:

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; 
    } 
} 
Các vấn đề liên quan