我正在尝试将此http://www.movable-type.co.uk/scripts/latlong.html中提供的代码段转换为java.但是我没有得到与网站相同的结果.这是我的代码,用于找到两个点之间的中点,其中给出了它们的纬度和经度
midPoint(12.870672,77.658964,12.974831,77.60935);
public static void midPoint(double lat1,double lon1,double lat2,double lon2)
{
double dLon = Math.toRadians(lon2-lon1);
double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),Math.sqrt( (Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By) );
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
System.out.print(lat3 +" " + lon3 );
}
Run Code Online (Sandbox Code Playgroud)
我不确定dLon是否正确.所以请帮助我们弄明白.PSI需要找到中点的纬度和经度
dog*_*ane 67
你需要转换为弧度.将其更改为以下内容:
public static void midPoint(double lat1,double lon1,double lat2,double lon2){
double dLon = Math.toRadians(lon2 - lon1);
//convert to radians
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
lon1 = Math.toRadians(lon1);
double Bx = Math.cos(lat2) * Math.cos(dLon);
double By = Math.cos(lat2) * Math.sin(dLon);
double lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);
//print out in degrees
System.out.println(Math.toDegrees(lat3) + " " + Math.toDegrees(lon3));
}
Run Code Online (Sandbox Code Playgroud)
leo*_*mer 12
使用Android Google Maps Utilities更加轻松:
LatLngBounds bounds = new LatLngBounds(start, dest);
bounds.getCenter();
Run Code Online (Sandbox Code Playgroud)
更新: 更好地使用构建器(为什么看到失败者答案):
LatLngBounds.builder().include(start).include(dest).build().getCenter();
Run Code Online (Sandbox Code Playgroud)