2011-05-09 112 views
10
double distance; 

Location locationA = new Location("point A"); 

locationA.setLatitude(latA); 
locationA.setLongitude(lngA); 

Location locationB = new Location("point B"); 

locationB.setLatitude(latB); 
LocationB.setLongitude(lngB); 

distance = locationA.distanceTo(locationB); 

上面的代码不起作用,我距离0.0 Km? 同样在位置类的构造函数中,字符串提供者是什么意思。 在上面的代码中,我使用PointA和PointB作为提供者。如何找到两个地点之间的距离?

为什么上面的代码无法正常工作?

谢谢你提前。

logcat的 05-09 17:48:56.144:INFO /电流LOC(1573):LAT 0.0 LNG 0.0 05-09 17:48:56.155:INFO/checklocation LOC(1573):LAT 54.4288665 LNG 10.169366

+1

你确定你的latA/latB和lngA/lngB的值是不同的,并且在正确的范围内?你能提供你正在使用的值的例子吗?此外,请注意'distanceTo'的结果是以米为单位,而不是千米。 – Dave 2011-05-09 12:48:54

+0

是的。我正在转换为米。我已经把logcat的结果。我得到的值是不同的位置...... – user590849 2011-05-09 13:01:52

+0

你不需要转换为米,结果已经在米。请发布完整的代码示例,因为我相信这里的问题不在您发布的代码中。 – Dave 2011-05-10 12:27:35

回答

14

尝试Location.distanceBetween(..)

更新:

如果你正在LAT从GeoPoint对象/ LON那么他们在微度。你必须乘以1e6。

+0

对于相同的输入,这会得到相同的结果。我相信在使用的输入中存在一个问题,从原始问题中未显示的代码。 – Dave 2011-05-10 12:35:43

+0

@ user590849 - 你可能从GeoPoint获得lon/lat吗?那些是微观的 - 你应该乘以1e6。 – 2011-05-10 12:38:43

1

实现上述的另一种方式,

public class Distance { 

    public static double distance(double lat1, double lon1, double lat2, double lon2) { 

     double theta = lon1 - lon2; 

     double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) 
       + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) 
       * Math.cos(deg2rad(theta)); 
     dist = Math.acos(dist); 
     dist = rad2deg(dist); 
     dist = dist * 60 * 1.1515; 
     //if (unit == "K") { 
     // dist = dist * 1.609344; 
     // else if (unit == "N") { 
     //dist = dist * 0.8684; 
     //} 
     return (dist); 
    } 


    public static final double PI = 3.14159265; 
    public static final double deg2radians = PI/180.0; 


    public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) { 

     double lat1 = latitude1 * deg2radians; 
     double lat2 = latitude2 * deg2radians; 
     double lon1 = longitude1 * deg2radians; 
     double lon2 = longitude2 * deg2radians; 
     // Williams gives two formulae; 
     // this is the more accurate for close distances. 
     // In practice, the two differed only in the 8th or 9th place, for 
     // separations as small as 1 degree. 
     double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2)/2), 
       2.0) 
       + Math.cos(lat1) 
       * Math.cos(lat2) 
       * Math.pow(Math.sin((lon1 - lon2)/2), 2.0))); 

     return radd; 
    } 





    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    /* :: This function converts decimal degrees to radians : */ 
    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    private static double deg2rad(double deg) { 
     return (deg * Math.PI/180.0); 
    } 

    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    /* :: This function converts radians to decimal degrees : */ 
    /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ 
    private static double rad2deg(double rad) { 
     return (rad * 180.0/Math.PI); 
    } 

} 
0
double CalculateDistance(double nLat1, double nLon1, double nLat2, double nLon2) 
{ 
    double nRadius = 6371; // Earth's radius in Kilometers 
    // Get the difference between our two points 
    // then convert the difference into radians 

    double nDLat = ToRad(nLat2 - nLat1); 
    double nDLon = ToRad(nLon2 - nLon1); 

    // Here is the new line 
    nLat1 = ToRad(nLat1); 
    nLat2 = ToRad(nLat2); 

    double nA = pow (sin(nDLat/2), 2) + cos(nLat1) * cos(nLat2) * pow (sin(nDLon/2), 2); 

    double nC = 2 * atan2(sqrt(nA), sqrt(1 - nA)); 
    double nD = nRadius * nC; 

    return nD; // Return our calculated distance 
} 

http://www.jaimerios.com/?p=39

+0

发布“distanceBetween(a,b)”这样的代码片段并不是一个答案。这里的问题是为什么'locationA.distanceTo(locationB)'(据称)给出了0米的结果? – Dave 2011-05-10 12:26:00

2

这不是答案,但请注意Location构造函数的签名是Location(String provider)

即你传递给构造函数字符串应该是LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDERLocationManager.PASSIVE_PROVIDER之一,"point A""point B"

16

只是一个快速的片段,因为我没有看到上面GeoPoints一个完整的,简单的解决方案:

public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) { 
    double lat1 = ((double)p1.getLatitudeE6())/1e6; 
    double lng1 = ((double)p1.getLongitudeE6())/1e6; 
    double lat2 = ((double)p2.getLatitudeE6())/1e6; 
    double lng2 = ((double)p2.getLongitudeE6())/1e6; 
    float [] dist = new float[1]; 
    Location.distanceBetween(lat1, lng1, lat2, lng2, dist); 
    return dist[0] * 0.000621371192f; 
} 

如果你想米,刚刚返回DIST [0]直接。

+0

完美的作品,谢谢! – jbc25 2013-01-29 21:20:37

9

如上所述,位置类是要走的路。这里是我用过的代码:

Location locationA = new Location("point A"); 

locationA.setLatitude(pointA.getLatitudeE6()/1E6); 
locationA.setLongitude(pointA.getLongitudeE6()/1E6); 

Location locationB = new Location("point B"); 

locationB.setLatitude(pointB.getLatitudeE6()/1E6); 
locationB.setLongitude(pointB.getLongitudeE6()/1E6); 

double distance = locationA.distanceTo(locationB); 

在这个例子中,pointA和pointB都是GeoPoint类的实例。