5

我对数学不是很好,但我需要计算标记的两个不同位置的距离。像这样的东西:

public double CalculationByDistance(double initialLat, double initialLong, double finalLat, double finalLong){

return distance;
}

或者有没有其他方法可以计算两个标记的距离,我也尝试用谷歌搜索答案..但找不到任何答案。

参考: http ://en.wikipedia.org/wiki/Haversine_formula

评论表示赞赏:) 谢谢!

4

5 回答 5

11

试试这个,比 Haversine 简单得多!

Location me   = new Location("");
Location dest = new Location("");

me.setLatitude(myLat);
me.setLongitude(myLong);

dest.setLatitude(destLat);
dest.setLongitude(destLong);

float dist = me.distanceTo(dest);
于 2013-07-22T12:18:01.843 回答
3

如果您想坚持使用 Haversine,请执行以下操作:

public double CalculationByDistance(double initialLat, double initialLong,
                                double finalLat, double finalLong){
    int R = 6371; // km (Earth radius)
    double dLat = toRadians(finalLat-initialLat);
    double dLon = toRadians(finalLong-initialLong);
    initialLat = toRadians(initialLat);
    finalLat = toRadians(finalLat);

    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(initialLat) * Math.cos(finalLat); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    return R * c;
}

public double toRadians(double deg) {
  return deg * (Math.PI/180);
}

此外,您需要创建一个方法 toRadians() 将值从度数转换为弧度,这很容易。希望能帮助到你!

于 2013-07-22T12:23:17.087 回答
1

从您的维基百科链接,直接应用公式,您可以执行以下操作:

public double CalculationByDistance(double initialLat, double initialLong, double finalLat, double finalLong){
    /*PRE: All the input values are in radians!*/

    double latDiff = finalLat - initialLat;
    double longDiff = finalLong - initialLong;
    double earthRadius = 6371; //In Km if you want the distance in km

    double distance = 2*earthRadius*Math.asin(Math.sqrt(Math.pow(Math.sin(latDiff/2.0),2)+Math.cos(initialLat)*Math.cos(finalLat)*Math.pow(Math.sin(longDiff/2),2)));

    return distance;

}
于 2013-07-22T12:24:58.963 回答
0

使用以下方法计算两个不同位置的距离。

 public double getKilometers(double lat1, double long1, double lat2, double long2) {
    double PI_RAD = Math.PI / 180.0;
    double phi1 = lat1 * PI_RAD;
    double phi2 = lat2 * PI_RAD;
    double lam1 = long1 * PI_RAD;
    double lam2 = long2 * PI_RAD;

    return 6371.01 * acos(sin(phi1) * sin(phi2) + cos(phi1) * cos(phi2) * cos(lam2 - lam1));
}
于 2019-06-17T06:33:38.680 回答
0
try this

/**
 * This is the implementation Haversine Distance Algorithm between two places
 * @author ananth
 * R = earth’s radius (mean radius = 6,371km)
 Δlat = lat2− lat1
 Δlong = long2− long1
 a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
 c = 2.atan2(√a, √(1−a))
 d = R.c
 *
 */

public class HaversineDistance {

/**
 * @param args
 * arg 1- latitude 1
 * arg 2 — latitude 2
 * arg 3 — longitude 1
 * arg 4 — longitude 2
 */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 final int R = 6371; // Radious of the earth
 Double lat1 = Double.parseDouble(args[0]);
 Double lon1 = Double.parseDouble(args[1]);
 Double lat2 = Double.parseDouble(args[2]);
 Double lon2 = Double.parseDouble(args[3]);
 Double latDistance = toRad(lat2-lat1);
 Double lonDistance = toRad(lon2-lon1);
 Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + 
 Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * 
 Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
 Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
 Double distance = R * c;

 System.out.println(“The distance between two lat and long is::” + distance);

}

 private static Double toRad(Double value) {
 return value * Math.PI / 180;
 }

}
于 2019-10-17T19:55:17.727 回答