我实施了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;
}
答案 0 :(得分:46)
以下是最终代码:
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;
}
答案 1 :(得分:16)
您的括号()
位于错误的位置。
您正在以弧度为单位添加度数,这将无效。 toDegrees()
将为你进行从弧度到度数的转换,然后一旦你有一个以度为单位的值,你就会进行标准化。
你有:
Math.toDegrees( (Math.atan2(y, x))+360 ) % 360;
但你需要:
( Math.toDegrees(Math.atan2(y, x)) + 360 ) % 360;
另请注意,Math.sin()
,Math.cos()
和所有其他三角函数的所有输入都必须以弧度为单位。如果您的输入是度数,则需要先使用Math.toRadians()
转换它们。
答案 2 :(得分:8)
从一个坐标到另一个坐标并找到北,东,南,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;
}
}
答案 3 :(得分: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;
}
}