# 百度地图如何计算两点之间距离

1.GeoPoint p1LL = new GeoPoint(39971802, 116347927);

2.GeoPoint p2LL = new GeoPoint(39892131, 116498555);

3.double distance = DistanceUtil.getDistance(p1LL, p2LL);

1. 1. static double DEF_PI = 3.14159265359; // PI
2. 2. static double DEF_2PI= 6.28318530712; // 2*PI
3. 3. static double DEF_PI180= 0.01745329252; // PI/180.0
4. 4. static double DEF_R =6370693.5; // radius of earth
5. 5. public double GetShortDistance(double lon1, double lat1, double lon2, double lat2)
6. 6. {
7. 7. double ew1, ns1, ew2, ns2;
8. 8. double dx, dy, dew;
9. 9. double distance;
10. 10. // 角度转换为弧度
11. 11. ew1 = lon1 * DEF_PI180;
12. 12. ns1 = lat1 * DEF_PI180;
13. 13. ew2 = lon2 * DEF_PI180;
14. 14. ns2 = lat2 * DEF_PI180;
15. 15. // 经度差
16. 16. dew = ew1 - ew2;
17. 17. // 若跨东经和西经180 度，进行调整
18. 18. if (dew > DEF_PI)
19. 19. dew = DEF_2PI - dew;
20. 20. else if (dew < -DEF_PI)
21. 21. dew = DEF_2PI + dew;
22. 22. dx = DEF_R * Math.cos(ns1) * dew; // 东西方向长度(在纬度圈上的投影长度)
23. 23. dy = DEF_R * (ns1 - ns2); // 南北方向长度(在经度圈上的投影长度)
24. 24. // 勾股定理求斜边长
25. 25. distance = Math.sqrt(dx * dx + dy * dy);
26. 26. return distance;
27. 27. }
28. 28. public double GetLongDistance(double lon1, double lat1, double lon2, double lat2)
29. 29. {
30. 30. double ew1, ns1, ew2, ns2;
31. 31. double distance;
32. 32. // 角度转换为弧度
33. 33. ew1 = lon1 * DEF_PI180;
34. 34. ns1 = lat1 * DEF_PI180;
35. 35. ew2 = lon2 * DEF_PI180;
36. 36. ns2 = lat2 * DEF_PI180;
37. 37. // 求大圆劣弧与球心所夹的角(弧度)
38. 38. distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2);
39. 39. // 调整到[-1..1]范围内，避免溢出
40. 40. if (distance > 1.0)
41. 41. distance = 1.0;
42. 42. else if (distance < -1.0)
43. 43. distance = -1.0;
44. 44. // 求大圆劣弧长度
45. 45. distance = DEF_R * Math.acos(distance);
46. 46. return distance;
47. 47. }
48. 48. double mLat1 = 39.90923; // point1纬度
49. 49. double mLon1 = 116.357428; // point1经度
50. 50. double mLat2 = 39.90923;// point2纬度
51. 51. double mLon2 = 116.397428;// point2经度
52. 52. double distance = GetShortDistance(mLon1, mLat1, mLon2, mLat2);