Chinaunix首页 | 论坛 | 博客
  • 博客访问: 860939
  • 博文数量: 182
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1766
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 11:49
文章分类

全部博文(182)

文章存档

2019年(1)

2016年(5)

2015年(29)

2014年(38)

2013年(21)

2012年(36)

2011年(52)

我的朋友

分类: Android平台

2014-01-22 10:03:33

这是android.location.Location中的计算方式,拿来用应该没问题,没试过

点击(此处)折叠或打开

  1. public static void distanceBetween(double startLatitude, double startLongitude,
  2.        double endLatitude, double endLongitude, float[] results) {
  3.        if (results == null || results.length < 1) {
  4.            throw new IllegalArgumentException("results is null or has length < 1");
  5.        }
  6.        computeDistanceAndBearing(startLatitude, startLongitude,
  7.            endLatitude, endLongitude, results);
  8.    }
  9.    
  10. private static void computeDistanceAndBearing(double lat1, double lon1,
  11.         double lat2, double lon2, float[] results) {
  12.         // Based on
  13.         // using the "Inverse Formula" (section 4)

  14.         int MAXITERS = 20;
  15.         // Convert lat/long to radians
  16.         lat1 *= Math.PI / 180.0;
  17.         lat2 *= Math.PI / 180.0;
  18.         lon1 *= Math.PI / 180.0;
  19.         lon2 *= Math.PI / 180.0;

  20.         double a = 6378137.0; // WGS84 major axis
  21.         double b = 6356752.3142; // WGS84 semi-major axis
  22.         double f = (a - b) / a;
  23.         double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);

  24.         double L = lon2 - lon1;
  25.         double A = 0.0;
  26.         double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
  27.         double U2 = Math.atan((1.0 - f) * Math.tan(lat2));

  28.         double cosU1 = Math.cos(U1);
  29.         double cosU2 = Math.cos(U2);
  30.         double sinU1 = Math.sin(U1);
  31.         double sinU2 = Math.sin(U2);
  32.         double cosU1cosU2 = cosU1 * cosU2;
  33.         double sinU1sinU2 = sinU1 * sinU2;

  34.         double sigma = 0.0;
  35.         double deltaSigma = 0.0;
  36.         double cosSqAlpha = 0.0;
  37.         double cos2SM = 0.0;
  38.         double cosSigma = 0.0;
  39.         double sinSigma = 0.0;
  40.         double cosLambda = 0.0;
  41.         double sinLambda = 0.0;

  42.         double lambda = L; // initial guess
  43.         for (int iter = 0; iter < MAXITERS; iter++) {
  44.             double lambdaOrig = lambda;
  45.             cosLambda = Math.cos(lambda);
  46.             sinLambda = Math.sin(lambda);
  47.             double t1 = cosU2 * sinLambda;
  48.             double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
  49.             double sinSqSigma = t1 * t1 + t2 * t2; // (14)
  50.             sinSigma = Math.sqrt(sinSqSigma);
  51.             cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
  52.             sigma = Math.atan2(sinSigma, cosSigma); // (16)
  53.             double sinAlpha = (sinSigma == 0) ? 0.0 :
  54.                 cosU1cosU2 * sinLambda / sinSigma; // (17)
  55.             cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
  56.             cos2SM = (cosSqAlpha == 0) ? 0.0 :
  57.                 cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)

  58.             double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
  59.             A = 1 + (uSquared / 16384.0) * // (3)
  60.                 (4096.0 + uSquared *
  61.                  (-768 + uSquared * (320.0 - 175.0 * uSquared)));
  62.             double B = (uSquared / 1024.0) * // (4)
  63.                 (256.0 + uSquared *
  64.                  (-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
  65.             double C = (f / 16.0) *
  66.                 cosSqAlpha *
  67.                 (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
  68.             double cos2SMSq = cos2SM * cos2SM;
  69.             deltaSigma = B * sinSigma * // (6)
  70.                 (cos2SM + (B / 4.0) *
  71.                  (cosSigma * (-1.0 + 2.0 * cos2SMSq) -
  72.                   (B / 6.0) * cos2SM *
  73.                   (-3.0 + 4.0 * sinSigma * sinSigma) *
  74.                   (-3.0 + 4.0 * cos2SMSq)));

  75.             lambda = L +
  76.                 (1.0 - C) * f * sinAlpha *
  77.                 (sigma + C * sinSigma *
  78.                  (cos2SM + C * cosSigma *
  79.                   (-1.0 + 2.0 * cos2SM * cos2SM))); // (11)

  80.             double delta = (lambda - lambdaOrig) / lambda;
  81.             if (Math.abs(delta) < 1.0e-12) {
  82.                 break;
  83.             }
  84.         }

  85.         float distance = (float) (b * A * (sigma - deltaSigma));
  86.         results[0] = distance;
  87.         if (results.length > 1) {
  88.             float initialBearing = (float) Math.atan2(cosU2 * sinLambda,
  89.                 cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
  90.             initialBearing *= 180.0 / Math.PI;
  91.             results[1] = initialBearing;
  92.             if (results.length > 2) {
  93.                 float finalBearing = (float) Math.atan2(cosU1 * sinLambda,
  94.                     -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
  95.                 finalBearing *= 180.0 / Math.PI;
  96.                 results[2] = finalBearing;
  97.             }
  98.         }
  99.     }


阅读(4362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~