用纬度和经度计算一段时间显示错误的距离

时间:2011-08-19 06:37:03

标签: android

我通过使用纬度和经度来计算两个地方之间的距离。有时它显示错误的距离。我需要计算以英里为单位的精确距离。能否请你帮忙。我应该如何在我的代码中进行更改。 这是我的代码。 lat2和lng2是我通过NETWORK PROVIDER / GPS拍摄的当前位置地理点。 lat1和lng2是目标地理点

                   double earthRadius = 3958.75;//earthRadius in miles
               // double earthRadius = 6371;
                double dLat = Math.toRadians(lat1-lat2);
                double dLng = Math.toRadians(lng1-lng2);
                double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                           Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lat1)) *
                           Math.sin(dLng/2) * Math.sin(dLng/2);
                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                double dist = earthRadius * c;//distance in miles

谢谢你..

2 个答案:

答案 0 :(得分:0)

试试这样。

 float results[] = new float[3];
    Location.distanceBetween(latitude_a, longitude_a, latitude_b,longitude_b, results);
    distance = results[0];

答案 1 :(得分:0)

     private double distance(double lat1, double lon1, double lat2, double lon2) {
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515;
       return (dist);
    }

   private double deg2rad(double deg) {
      return (deg * Math.PI / 180.0);
    }
   private double rad2deg(double rad) {
      return (rad * 180.0 / Math.PI);
    }