你如何计算GPS JAVA中两点之间的距离?

时间:2012-05-02 08:52:15

标签: java

  

可能重复:
  How do I calculate distance between two latitude longitude points?

如何计算JAVA中纬度和经度值两点之间的距离?

由于

1 个答案:

答案 0 :(得分:0)

看看这个:http://www.zipcodeworld.com/samples/distance.java.html

由于上述链接已被破解,请查看:Calculate distance between two latitude-longitude points? (Haversine formula)

这篇文章的摘要:

public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
public int calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {

    double latDistance = Math.toRadians(userLat - venueLat);
    double lngDistance = Math.toRadians(userLng - venueLng);

    double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
                    (Math.cos(Math.toRadians(userLat))) *
                    (Math.cos(Math.toRadians(venueLat))) *
                    (Math.sin(lngDistance / 2)) *
                    (Math.sin(lngDistance / 2));

    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

    return (int) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));

}