如何找到两个地理点之间的距离?

use*_*849 11 android geolocation

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);  
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,我距离0.0公里?同样在location类的构造函数中,字符串提供程序的含义是什么.在上面的代码中,我使用PointA和PointB作为提供者.

为什么上面的代码不起作用?

先感谢您.

LOGCAT 05-09 17:48:56.144:INFO/current loc(1573):lat 0.0 lng 0.0 05-09 17:48:56.155:INFO/checklocation loc(1573):lat 54.4288665 lng 10.169366

Gle*_*len 17

只是一个快速的片段,因为我没有看到上面有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;
}
Run Code Online (Sandbox Code Playgroud)

如果你想要米,只需直接返回dist [0].


Pet*_*ego 14

尝试Location.distanceBetween(..)

更新:

如果你从GeoPoint获得lat/lon,那么他们是微观的.你必须乘以1e6.


小智 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);
Run Code Online (Sandbox Code Playgroud)

在此示例中,pointA和pointB都是GeoPoint类的实例.