2013-01-15 26 views
4

我正在開發AndroidMapView並開發基於地圖的應用程序。我需要從特定的Co-ordinates找到X距離。方向不是我的優先距離是我的首要任務說,我需要找到100米從一個特定的位置任何想法我怎麼能做到這一點 預先感謝閱讀和回答。Android從已定義的位置查找X點的緯度經度

回答

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; 
}; 
+0

我該如何使用這個與geopoints? –

+1

只需將位置轉換爲地理位置: int lat =(int)(location.getLatitude()* 1E6); int lng =(int)(location.getLongitude()* 1E6); GeoPoint point = new GeoPoint(lat,lng); – javram

+0

我不明白軸承是如何工作的?它是在0-360之間嗎?這似乎並不奏效。謝謝 – aidanmack

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; 
} 
相關問題