【问题标题】:How can I check the distance between two latitude and longitude coorindates? [duplicate]如何检查两个经纬度坐标之间的距离? [复制]
【发布时间】:2013-03-27 12:40:08
【问题描述】:

我正在使用这个 sn-p 获取用户的最佳当前位置:

    LocationManager ltrLocationer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    Location locLocation = ltrLocationer.getLastKnownLocation(ltrLocationer.getBestProvider(criteria, true));

我有两个 Double 对象,分别包含一个纬度和一个经度。我想检查当前坐标与上述值之间的距离(以米为单位)。我该怎么做?

这听起来很简单,但我找不到一个例子来说明这一点。谢谢。

【问题讨论】:

  • 你是对的。我建议关闭它。

标签: java android android-location


【解决方案1】:

Location 对象有一个距离方法。使用它,它会为您计算。如果您的坐标是双精度,请使用静态函数 Location.distanceBetween

【讨论】:

    【解决方案2】:

    看看这里:GPS Coordinates to Meters 这里还有一篇关于计算公式的维基百科文章:Haversine formula

    【讨论】:

      【解决方案3】:

      检查这个方法,这会给你distance in a straight line

      public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
      double earthRadius = 3958.75;
      double dLat = Math.toRadians(lat2-lat1);
      double dLng = Math.toRadians(lng2-lng1);
      double sindLat = Math.sin(dLat / 2);
      double sindLng = Math.sin(dLng / 2);
      double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
              * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      double dist = earthRadius * c;
      
      return dist;
      }
      

      如果你想要road distance,这个操作有点困难,需要调用Google Direction API 服务。

      【讨论】:

        猜你喜欢
        • 2011-09-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-05
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        相关资源
        最近更新 更多