【问题标题】:distance between two locations is not right?两个位置之间的距离不对?
【发布时间】:2014-07-12 15:03:22
【问题描述】:

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

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 公里

【问题讨论】:

  • 你怎么知道diszance不是真的?你从哪里得到的 85 公里?您是否通过允许计算距离的网页检查了距离?

标签: android location distance


【解决方案1】:

使用静态方法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

【讨论】:

    【解决方案2】:

    您可以为此使用标准功能:

    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

    【讨论】:

      猜你喜欢
      • 2011-03-16
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      相关资源
      最近更新 更多