本文介绍了我如何计算Java中两个gps点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了这段代码,但它不起作用:

I used this code but it doesnt work:

需要两个gps坐标之间的距离,如41.1212,11.2323公里(Java)

Need the distance between two gps coordinates like 41.1212, 11.2323 in kilometers (Java)

double d2r = (180 / Math.PI);
double distance = 0;

try{
    double dlong = (endpoint.getLon() - startpoint.getLon()) * d2r;
    double dlat = (endpoint.getLat() - startpoint.getLat()) * d2r;
    double a =
        Math.pow(Math.sin(dlat / 2.0), 2)
            + Math.cos(startpoint.getLat() * d2r)
            * Math.cos(endpoint.getLat() * d2r)
            * Math.pow(Math.sin(dlong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = 6367 * c;

    return d;

} catch(Exception e){
    e.printStackTrace();
}


推荐答案

经度和纬度度(0-360)。如果你想把度数转换成弧度(0-2π),你需要除以360并乘以2ππ,(或者相当于,乘以π/ 180 )。然而,在你的代码中,你乘以180 //

Longitude and latitude are given in degrees (0-360). If you want to convert degrees to radians (0-2π) you need to divide by 360 and multiply by 2π, (or equivalently, multiply by π/180). In your code however, you multiply by 180/π.

也就是说,改变

That is, change

double d2r = (180 / Math.PI);

转换为

double d2r = Math.PI / 180;

这篇关于我如何计算Java中两个gps点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:46