2014-05-01 195 views
0

给定开始的纬度和经度,方位和距离,我试图计算结束位置的坐标(在Android中)。所以你在x,y并且面向z度(实际上是弧度)的方向。如果您向前移动d米,那么您的新位置是什么?我发现了一个公式,这样做的,这里是我的代码:计算坐标

private double[] move(Location location, float bearingRadians, double meters){ 
     // Formula from: http://www.movable-type.co.uk/scripts/latlong.html 
     double lat1 = location.getLatitude(); 
     double lon1 = location.getLongitude(); 
    double distanceRadians = meters/EARTH_RADIUS_METERS; 

    double newLat = Math.asin(Math.sin(lat1) * Math.cos(distanceRadians) + Math.cos(lat1) * Math.sin(distanceRadians) * Math.cos(bearingRadians)); 
    double newLon = lon1 + Math.atan2(Math.sin(bearingRadians) * Math.sin(distanceRadians) * Math.cos(lat1), Math.cos(distanceRadians) - Math.sin(lat1) * Math.sin(newLat)); 

    Log.d(TAG,"Bearing: " + bearingRadians + ", distance: " + meters); 
    Log.d(TAG,"Latitude changed from " + lat1 + " to " + newLat); 
    Log.d(TAG,"Longitude changed from " + lon1 + " to " + newLon); 

    return new double[]{newLat,newLon}; 
} 

下面是输出:

Bearing: -2.9843714, distance: 7.0 
Latitude changed from 43.8 to -0.18229823611594947 
Longitude changed from -103.05 to -103.05000017504125 

(注:不是我的实际位置,如果你很好奇!)注意,经度非常改变有点像我所期望的那样,因为我只有7米。但纬度发生了什么?我曾尝试将输入和输出转换为弧度和度数,以防万一这是问题,但事实并非如此。这些转换都不会将纬度数字带到应该在的范围附近。

评论有我得到公式的网站,但也有很多其他地方有它,我已经检查了纬度公式对一堆,直到我的眼睛受伤,我看不到任何问题。有人看到这个问题?

回答

0

我没有分析公式的问题到底在哪里隐藏。

但是,如果您只是寻找一个工作示例,您可以在this SO question的接受答案中找到Android代码。

+0

谢谢你,工作!我从来没有在我的搜索中找到那篇文章。 – user1854994