请解释一下这种情况
现在我有两个数组,具有附近地点的纬度和经度,还有用户位置纬度和经度现在我想计算用户位置和附近地点之间的距离,并希望在 ListView 中显示它们。
我知道有一种计算距离的方法
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results);
现在的问题是如何在这个方法中通过这两个具有附近纬度和经度的数组并获得距离数组。
最佳答案
http://developer.android.com/reference/android/location/Location.html
看远方
Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.
或距离之间
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
您可以根据纬度和经度创建 Location 对象:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
或
private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) {
float pk = (float) (180.f/Math.PI);
float a1 = lat_a / pk;
float a2 = lng_a / pk;
float b1 = lat_b / pk;
float b2 = lng_b / pk;
double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
double t3 = Math.sin(a1) * Math.sin(b1);
double tt = Math.acos(t1 + t2 + t3);
return 6366000 * tt;
}
关于android - 计算两个地理位置之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8049612/