两个地点之间的距离不对?

时间:2014-07-12 15:03:23

标签: android location distance

我使用这个获得两个位置:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

然后我计算它们之间的距离,但距离不是真的

public static float calculateDistance(double e, double f, double g, double h)
{
    float dLat = (float) Math.toRadians(g - e);
    float dLon = (float) Math.toRadians(h - f);
    float a =
            (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(e))
                    * Math.cos(Math.toRadians(g)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
    float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
    float d = 6371 * c;
    return d;
}

(32.2163799,35.0420986) (31.9210915,35.2037014)

结果36.193707公里 但实际情况不止于此,大约85公里

2 个答案:

答案 0 :(得分:0)

您可以使用标准功能:

Location location1 = new Location("<provider name");
location1.setLatitude(lat1);
location1.setLongitude(lng1);

Location location2 = new Location("<provider name");
location2.setLatitude(lat2);
location2.setLongitude(lng2);

float distance = location1.distanceTo(location2);

更多信息:http://developer.android.com/reference/android/location/Location.html#distanceTo%28android.location.Location%29

或功能之间的距离: http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double,%20double,%20float[]%29

答案 1 :(得分:0)

使用静态方法distanceBetween()获取以米为单位的距离

float[] results = new float[3];
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
float distanceInMeters = results[0]


请注意,结果数组将几个与两点之间距离相关的值打包在一起:

  - results[0] -  distance in meters
  - results[1] -  initial bearing angle of the shortest path between them
  - results[2] -  final bearing angle of the shortest path between them