使用Haversine公式创建一个方法,Android V2。

5

我不太擅长数学,但我需要计算标记的两个不同位置之间的距离。就像这样:

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

return distance;
}

还有其他的方法可以计算两个标记的距离吗?我已经尝试过谷歌搜索答案,但是没有找到任何结果。

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

欢迎评论 :) 谢谢!


这可能会解决你的问题:https://dev59.com/Z1_Va4cB1Zd3GeqPW8kG#9045073 - Stefan
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);

但这只是用于计算从一个点到另一个点的距离,没有考虑建筑物、障碍等因素吧? - Jack
哦,有没有什么方法我也可以实现那个函数?还是不可能的? - Jack
1
这是可能的,毫无疑问,但它不像上面的代码那样容易。 这就是直线距离。 - Scott Helme
Google Directions API 是你所需要的:https://developers.google.com/maps/documentation/directions/ - Scott Helme
啊,好吧。总比没有强,哈哈,谢谢你的帮助 :) 我会尝试一下的。 - Jack

4
如果您想坚持使用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() 的方法来将角度值转换为弧度值,这非常简单。 希望能对您有所帮助!

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;

}

这里的距离单位是什么?是英里、千米、米等吗?因此,以千米为单位的地球半径将提供以千米为单位的距离? - Pranesh Janarthanan

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));
}

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;
 }

}

你能否提供一些关于它的信息,而不是只是原样发布它? - csabinho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接