Android从定义的位置查找X点的纬度经度

时间:2013-01-15 06:38:30

标签: android google-maps location distance

我正在处理Android MapView并开发基于地图的应用程序。我需要找到特定Co-ordinates的X距离。方向不是我的优先考虑距离是我的优先考虑我要说我需要找到距离特定位置100米的任何想法,我该怎么做 提前感谢您的阅读和回答。

2 个答案:

答案 0 :(得分:7)

为了计算在离原点一定距离的线上找到一个点,你需要有一个方位(或方向)以及距离。这是一个函数,它将获取起始位置,方位和距离(深度)并返回目标位置(对于Android):您可能希望将其从KM转换为Meters或其他任何内容。

public static Location GetDestinationPoint(Location startLoc, float bearing, float depth) 
{ 
    Location newLocation = new Location("newLocation");

    double radius = 6371.0; // earth's mean radius in km 
    double lat1 = Math.toRadians(startLoc.getLatitude()); 
    double lng1 = Math.toRadians(startLoc.getLongitude()); 
    double brng = Math.toRadians(bearing); 
    double lat2 = Math.asin( Math.sin(lat1)*Math.cos(depth/radius) + Math.cos(lat1)*Math.sin(depth/radius)*Math.cos(brng) ); 
    double lng2 = lng1 + Math.atan2(Math.sin(brng)*Math.sin(depth/radius)*Math.cos(lat1), Math.cos(depth/radius)-Math.sin(lat1)*Math.sin(lat2)); 
    lng2 = (lng2+Math.PI)%(2*Math.PI) - Math.PI;  

    // normalize to -180...+180 
    if (lat2 == 0 || lng2 == 0) 
    {
        newLocation.setLatitude(0.0);
        newLocation.setLongitude(0.0);
    }
    else
    {
        newLocation.setLatitude(Math.toDegrees(lat2));
        newLocation.setLongitude(Math.toDegrees(lng2));
    }

    return newLocation;
};

答案 1 :(得分:1)

只是将javram的答案换成米和弧度而不是度数。

/**
 * Create a new location specified in meters and bearing from a previous location.
 * @param startLoc from where
 * @param bearing which direction, in radians from north
 * @param distance meters from startLoc
 * @return a new location
 */
public static Location createLocation(Location startLoc, double bearing, double distance) {
    Location newLocation = new Location("newLocation");

    double radius = 6371000.0; // earth's mean radius in m
    double lat1 = Math.toRadians(startLoc.getLatitude());
    double lng1 = Math.toRadians(startLoc.getLongitude());
    double lat2 = Math.asin(Math.sin(lat1) * Math.cos(distance / radius) + Math.cos(lat1) * Math.sin(distance / radius) * Math.cos(bearing));
    double lng2 = lng1 + Math.atan2(Math.sin(bearing) * Math.sin(distance / radius) * Math.cos(lat1), Math.cos(distance / radius) - Math.sin(lat1) * Math.sin(lat2));
    lng2 = (lng2 + Math.PI) % (2 * Math.PI) - Math.PI;

    // normalize to -180...+180
    if (lat2 == 0 || lng2 == 0) {
        newLocation.setLatitude(0.0);
        newLocation.setLongitude(0.0);
    } else {
        newLocation.setLatitude(Math.toDegrees(lat2));
        newLocation.setLongitude(Math.toDegrees(lng2));
    }

    return newLocation;
}
相关问题