计算由lat表示的两点之间的距离,长达15英尺精度

use*_*263 2 java gps distance

我已将公式转换为此处提供的Java .但准确性是一个问题.我们正在使用GPS坐标.

我们正在使用iPhone提供的GPS位置,精度高达10小时.

/*
 * Latitude and Longitude are in Degree
 * Unit Of Measure : 1 = Feet,2 = Kilometer,3 = Miles
 */
//TODO 3 Change Unit of Measure and DISTANCE_IN_FEET constants to Enum
public static Double calculateDistance(double latitudeA,double longitudeA,double latitudeB,double longitudeB,short unitOfMeasure){

    Double distance;

    distance = DISTANCE_IN_FEET * 
               Math.acos(       

                               Math.cos(Math.toRadians(latitudeA)) * Math.cos(Math.toRadians(latitudeB)) 
                           *
                               Math.cos(Math.toRadians(longitudeB) - Math.toRadians(longitudeA))
                           +
                               Math.sin(Math.toRadians(latitudeA))
                           *
                               Math.sin(Math.toRadians(latitudeB))

                       );

    return distance;

}
Run Code Online (Sandbox Code Playgroud)

仅供参考:public static final int DISTANCE_IN_FEET = 20924640;

然后我使用Math.round(距离); 转换为长.

对于实际的25英尺,我输出7英尺.

Boh*_*ian 17

你需要hasrsine配方.

这是我的java实现:

/**
 * Calculates the distance in km between two lat/long points
 * using the haversine formula
 */
public static double haversine(
        double lat1, double lng1, double lat2, double lng2) {
    int r = 6371; // average radius of the earth in km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = 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(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = r * c;
    return d;
}
Run Code Online (Sandbox Code Playgroud)