【问题标题】:Get distance between two different latitude and longitude in android?在android中获取两个不同纬度和经度之间的距离?
【发布时间】:2016-11-13 05:42:06
【问题描述】:

我用过locationA.distanceTo(locationB); 并且还复制粘贴了为 haversine 公式编写的方法。

两个地理点之间的实际距离是 25m,但我得到一个 使用上述方法得到 6894.52192658389 的结果。

关注了这个:Creating a method using Haversine Formula, Android V2 还有这个Find distance between two points on map using Google Map API V2

使用此免费工具检查距离http://www.onlineconversion.com/map_greatcircle_distance.htm

【问题讨论】:

    标签: java android google-maps-android-api-2 haversine


    【解决方案1】:

    你可以试试下面的代码:

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

    它会给你以米为单位的结果,你可以转换成任何需要的单位。

    【讨论】:

    • 您好,此方法使用Haversine公式,返回值6894.52192658389
    • @NishonTandukar 你能给我坐标和预期的结果吗?
    • 原来我很笨拙,忘记在目标位置设置 tLatitute
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2013-07-30
    • 1970-01-01
    • 2013-10-25
    • 2018-03-25
    • 2018-06-11
    相关资源
    最近更新 更多