【问题标题】:Calculate distance between two locations in metre以米为单位计算两个位置之间的距离
【发布时间】:2017-08-22 03:55:57
【问题描述】:

我有两个位置,我想以米为单位计算距离。我写了一些代码,但它不能完美地工作。

private void getDistanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2)
{
    Location loc1 = new Location("");
    loc1.setLatitude(lat1);
    loc1.setLongitude(lon1);

    Location loc2 = new Location("");
    loc2.setLatitude(lat2);
    loc2.setLongitude(lon2);

    int R = 6371; // km

    double dLat = deg2rad(lat2-lat1);
    double dLon = deg2rad(lon2-lon1);
    double  a =
            Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                            Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double distanceInMeters = R * c;
    Log.e("distanceInMeters",distanceInMeters/10000+"mm");
}

public double  deg2rad(double deg) {
    return deg * (Math.PI/180);
}

如何以米为单位计算距离?我的目标是,如果仪表 > 200 做某事。我该如何解决我的问题?

【问题讨论】:

    标签: android gps android-location android-gps


    【解决方案1】:

    没有必要为此重新发明轮子。

    您可以只使用Location.distanceBetween() 方法。

    来自the documentation

    计算两个位置之间的近似距离(以米为单位)

    这是一个简单的例子:

    private float getDistanceBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2) {
    
        float[] distance = new float[2];
    
        Location.distanceBetween( lat1, lon1,
                lat2, lon2, distance);
    
        return distance[0];
    }
    

    如果结果数组不仅填充了索引零,则其他索引每个都包含一个方位(初始和最终)。

    方位是指定罗盘方向的数字。

    来自http://www.geomidpoint.com/destination/help.html

    方位角(或方位角)是指南针从 起点,必须在 0 到 360 范围内。0 代表 北,东90,南180,西270。

    【讨论】:

    • 它对我有用。请解释一下什么是距离[1]?
    • @ANDY_VAR 你可以在文档链接中看到解释:The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].
    • 感谢您的回复。但我想知道它的实际意义。我们得到两个距离是什么情况。我也想知道在这种情况下是什么意思
    • @ANDY_VAR 我刚刚用额外的信息更新了答案。这里还有更多信息,特别是关于初始和最终轴承的信息:reddit.com/r/answers/comments/gyfba/…
    • 请更正方法名称的拼写错误。正确的是 getDistanceBetweenTwoPoints
    【解决方案2】:

    使用此方法计算两个纬度/经度点之间的距离及其高程。

    /**
     * Calculate distance between two points in latitude and longitude taking
     * into account height difference using the Haversine method as its base.
     *
     * Elevation should be in meters. If you are not interested in elevation, pass 0.
     *
     * @return Distance in meters
     */
    private double distanceBetween(double lat1, double lat2, double lon1,
                                   double lon2, double el1, double el2) {
    
        final int R = 6371; // Radius of the earth
    
        double latDistance = Math.toRadians(lat2 - lat1);
        double lonDistance = Math.toRadians(lon2 - lon1);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // convert to meters
    
        double height = el1 - el2;
    
        distance = Math.pow(distance, 2) + Math.pow(height, 2);
    
        return Math.sqrt(distance);
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用以下代码来计算两个位置之间的距离(以米为单位):

      private double distanceBetween(double lat1, double lon1, double lat2, double lon2) {
          double theta = lon1 - lon2;
          double dist = Math.sin(deg2rad(lat1))
                  * Math.sin(deg2rad(lat2))
                  + Math.cos(deg2rad(lat1))
                  * Math.cos(deg2rad(lat2))
                  * Math.cos(deg2rad(theta));
          dist = Math.acos(dist);
          dist = dist * 180.0 / Math.PI;
          dist = dist * 60 * 1.1515*1000;
          return (dist);
      }
      
      private double deg2rad(double deg) {
          return (deg * Math.PI / 180.0);
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-19
        • 2015-02-26
        • 2015-10-26
        • 2012-05-13
        • 2011-12-24
        • 2018-09-21
        • 2016-02-27
        相关资源
        最近更新 更多