android - 计算两个地理位置之间的距离

标签 android location distance latitude-longitude

请解释一下这种情况

现在我有两个数组,具有附近地点的纬度和经度,还有用户位置纬度和经度现在我想计算用户位置和附近地点之间的距离,并希望在 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/

相关文章:

android - Android 中 3G 相邻小区的 cellID 和 LAC/PSC

algorithm - 计算比给定距离更近的点数

r - 填充R中距离矩阵中缺失的行/列

android - .svg 文件作为 HTML 中的对象未在 Android WebVIew 中显示

java - 无法解析方法 `setLatestEventInfo`

Java:重新定位绘制到 JFrame 中的 Canvas 时出现问题

Android LocationClient.getLastLocation() 返回带有新时间戳的旧的和不准确的位置

jquery - 使用 Google Maps API 计算 "As the crow flies"距离

java - 为什么我得到一个 null 对象?

android - cordova构建android给出错误 "The system cannot find the path specified."