计算两个位置之间的距离。仅当经度更改时值才会更改

发布于 2025-01-03 19:40:07 字数 889 浏览 3 评论 0原文

我正在使用这段代码来计算两点之间的距离。我正在使用半正弦公式,

private double CalcDistance(Location Start, Location End) 
    {
        // TODO Auto-generated method stub
        double distance;
        double lat1 = Start.getLatitude();
        double lat2 = End.getLatitude();
        double lng1 = Start.getLongitude();
        double lng2 = End.getLongitude();
        double dLat = Math.abs(Math.toRadians(lat2-lat1));
        double dLng = Math.abs(Math.toRadians(lng2 - lng1));

        distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

        double c = 2 * Math.asin(Math.sqrt(distance));

        //Return the answer in Kilometre (6371km the mean radius of the earth)
        return c * 6371;
    }

仅当位置的经度更改时生成的值才会更改,并且如果纬度更改则保持不变。我的代码只这样做有什么原因吗?

I am using this code to calculate the the distance between two points. I am using the Haversine formula

private double CalcDistance(Location Start, Location End) 
    {
        // TODO Auto-generated method stub
        double distance;
        double lat1 = Start.getLatitude();
        double lat2 = End.getLatitude();
        double lng1 = Start.getLongitude();
        double lng2 = End.getLongitude();
        double dLat = Math.abs(Math.toRadians(lat2-lat1));
        double dLng = Math.abs(Math.toRadians(lng2 - lng1));

        distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

        double c = 2 * Math.asin(Math.sqrt(distance));

        //Return the answer in Kilometre (6371km the mean radius of the earth)
        return c * 6371;
    }

The valus that is produced only changes if the Longitude of the Location is changed and stays the same if the Latitude is changed. Is there any reason my code only does this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2025-01-10 19:40:07

嘿,我不完全确定,但只要看看你的代码,你可能犯了一个错误,我不明白,但也许你可以

distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

改为

distance = 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);

Hey I'm not entirely sure, but just looking at your code you might have made an error, I dont understand it but perhaps you could change

distance = Math.sin(dLat/2) * Math.sin(dLng/2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * 
                Math.sin(dLng/2) * Math.sin(dLng/2);

to

distance = 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);
你是我的挚爱i 2025-01-10 19:40:07

你需要更改

Math.sin(dLat/2) * Math.sin(dLng/2)

Math.sin(dLat/2) * Math.sin(dLat/2)

You need to change

Math.sin(dLat/2) * Math.sin(dLng/2)

to

Math.sin(dLat/2) * Math.sin(dLat/2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文