從一個軸承座標到另一個,找到北,東,南,weast :)
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;
}
}
權!但仍然輸入參數是 ProcJobLocation.bearing(53.944592,27.595215,55.745752,37.630768); 和輸出是359.11592632310266。 仍然有一些錯誤。 – 2012-02-26 23:03:44
您的輸入似乎是度數。你需要使用'Math.toRadians()'將它們轉換爲弧度,否則'Math.sin()','Math.cos()'等會給出錯誤的結果。 – DNA 2012-02-26 23:07:00
謝謝你的幫助! – 2012-02-27 09:33:14