【问题标题】:how to find the correct distance between two geopoints in map?如何找到地图中两个地理点之间的正确距离?
【发布时间】:2012-07-02 21:34:29
【问题描述】:

我需要开发应用程序,用户必须在其中找到他停放的汽车并显示他与停放汽车之间的距离。我使用 GPS 和定位服务。

对于距离,我使用了 haversine 公式,但距离始终显示 0 米。

我尝试了很多在谷歌搜索解决方案,但没有得到任何正确的解决方案。

谁能给点建议?

【问题讨论】:

标签: android android-location


【解决方案1】:

Google Docs 有两种方法

如果您从 GeoPoint 获取纬度/经度,那么它们是微度数。你必须乘以 1e6。

但我更喜欢使用下面的方法。 (它基于Haversine 公式)

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

double dist = GeoUtils.distanceKm(mylat, mylon, lat, lon);

 /**
 * Computes the distance in kilometers between two points on Earth.
 * 
 * @param lat1 Latitude of the first point
 * @param lon1 Longitude of the first point
 * @param lat2 Latitude of the second point
 * @param lon2 Longitude of the second point
 * @return Distance between the two points in kilometers.
 */

public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
    int EARTH_RADIUS_KM = 6371;
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM;
}

最后我想分享奖金信息。

如果您正在寻找驾车路线,请在两个地点之间行驶,然后前往

http://code.google.com/p/j2memaprouteprovider/

【讨论】:

  • @VipulShah - 我认为这给出了两个地理点之间的直线距离,而不是方向或行驶距离,对吧?
  • @user370305 完美!!如果您需要行驶距离和路径,请参阅code.google.com/p/j2memaprouteprovider
  • @vipul,但是通常在 android 应用程序中显示的距离是多少?直行/开车?
  • @Archie.bpgc 我认为它是直...您可以选择查看行驶距离
【解决方案2】:

distanceBetween() 方法将为您提供两点之间的直线距离。得到两点之间的路线距离见我的回答 Here

【讨论】:

    【解决方案3】:

    尝试在 android.location API 中使用此方法

    distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

    此方法计算两个位置之间的近似距离(以米为单位),以及它们之间最短路径的初始方位和最终方位

    注意:如果您从 GeoPoint 获取纬度/经度,那么它们是微度数。你必须乘以 1E6

    如果您想通过 Haversine 公式

    计算 2 个 Geopoint 之间的距离
    public class DistanceCalculator {
       // earth’s radius = 6,371km
       private static final double EARTH_RADIUS = 6371 ;
       public static double distanceCalcByHaversine(GeoPoint startP, GeoPoint endP) {
          double lat1 = startP.getLatitudeE6()/1E6;
          double lat2 = endP.getLatitudeE6()/1E6;
          double lon1 = startP.getLongitudeE6()/1E6;
          double lon2 = endP.getLongitudeE6()/1E6;
          double dLat = Math.toRadians(lat2-lat1);
          double dLon = Math.toRadians(lon2-lon1);
          double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
          Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
          Math.sin(dLon/2) * Math.sin(dLon/2);
          double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
          return EARTH_RADIUS * c;
       }
    }
    

    【讨论】:

      【解决方案4】:

      android.location.Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] 结果)

      Geopoints 有 getLongitudeE6() 和 getLatitudeE6() 来提供帮助。请记住,这些是 E6,因此您需要除以 1E6。

      【讨论】:

        【解决方案5】:

        harvesine 公式的问题在于它不能计算实际距离。它是球体上两点的距离。实际距离取决于街道或水路。 harvesine 公式也有点复杂,因此更容易让 Google-Api 给出真实距离。使用 Googlemaps Api,您需要学习路线 api。

        【讨论】:

          【解决方案6】:

          distanceBetween 不指真实距离(道路距离) 所以我建议你访问这个谷歌源代码,它会告诉你两个地理点之间的真实道路距离。 Link 有 2 个版本,一个适用于 Android,一个适用于 blackberry,请查看

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-20
            • 1970-01-01
            • 2012-04-29
            • 1970-01-01
            • 2011-02-14
            • 2011-12-20
            相关资源
            最近更新 更多