1

我想以公里为单位显示两个地方的距离,为此我编写了以下代码:

hyd(17.38,78.48) eluru(16.7,81.1)
 private String getDistance(double lat1, double lat2, double lon1, double lon2) {
               double Radius = 6371;
               double dLat = Math.toRadians(lat2 - lat1);
                double dLon = Math.toRadians(lon2 - lon1);
                double a = Math.sin(dLat / 5) * Math.sin(dLat / 5)
                        + Math.cos(Math.toRadians(lat1))
                        * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 5)
                        * Math.sin(dLon / 5);
                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                double km = Radius * c;

                String kms=Double.toString(km);
                return kms;

                }

输出 = 2995.8772

正确的 o/p = 330km

我怎样才能得到android中两个地方之间的确切距离提前谢谢.....

4

1 回答 1

2

使用此方法

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results);

例子

 public static double CalculateDistance(double lat1, double lng1, double lat2, double lng2) {
 float[] result=new float[1];
 Location.distanceBetween (lat1,lng1,lat2, lng2,  result);
 return (double)result[0]/1000; // in km
}
于 2012-11-03T12:48:38.333 回答