计算经纬度距离
?
前兩天遇到通過騰訊地圖得到GPS坐標,然后計算總距離的問題,發現很多地圖都是不一樣的,然后在網上看到一篇帖子,忘了是哪篇了,這里記下來,給以后自己看,作者看到的話可以通知我刪掉。
?
?
/*** 計算經緯度距離***/ /** * 各地圖API坐標系統比較與轉換; * WGS84坐標系:即地球坐標系,國際上通用的坐標系。設備一般包含GPS芯片或者北斗芯片獲取的經緯度為WGS84地理坐標系, * 谷歌地圖采用的是WGS84地理坐標系(中國范圍除外); * GCJ02坐標系:即火星坐標系,是由中國國家測繪局制訂的地理信息系統的坐標系統。由WGS84坐標系經加密后的坐標系。 * 谷歌中國地圖和搜搜中國地圖采用的是GCJ02地理坐標系; BD09坐標系:即百度坐標系,GCJ02坐標系經加密后的坐標系; * 搜狗坐標系、圖吧坐標系等,估計也是在GCJ02基礎上加密而成的。 */ public class LocationUtils { private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math.PI / 180.0; } // a = 6369000.0, 1/f = 298.3// f = 0.0033523298692591// 1 - f = 0.9966476701307409// b = a * (1 - f)// b = 6347649.011062689// ee = (a^2 - b^2) / a^2;// a^2 = 40564161000000// b^2 = 40292647967645.13public static double PI = 3.1415926535897932384626;public static double R = 6369000.0;// 地球半徑public static double EE = 0.006693421622966021;/*** 84 to 火星坐標系 (GCJ-02) World Geodetic System ==> Mars Geodetic System* * @param lng* @param lng* @return Gps*/public static Gps gps84ToGcj02(double lng, double lat) {if (outOfChina(lng, lat)) {return null;}double dLat = transformLat(lng - 105.0, lat - 35.0);double dLon = transformLng(lng - 105.0, lat - 35.0);double radLat = lat / 180.0 * PI;double magic = Math.sin(radLat);magic = 1 - EE * magic * magic;double sqrtMagic = Math.sqrt(magic);dLat = (dLat * 180.0) / ((R * (1 - EE)) / (magic * sqrtMagic) * PI);dLon = (dLon * 180.0) / (R / sqrtMagic * Math.cos(radLat) * PI);double mgLat = lat + dLat;double mgLon = lng + dLon;return new Gps(mgLon, mgLat);}/*** * 火星坐標系 (GCJ-02) to 84 * ** * @param lng* @param lat* @return Gps*/public static Gps gcj02ToGps84(double lng, double lat) {Gps gps = transform(lng, lat);double lontitude = lng * 2 - gps.getLng();double latitude = lat * 2 - gps.getLat();return new Gps(lontitude, latitude);}/*** 火星坐標系 (GCJ-02) 與百度坐標系 (BD-09) 的轉換算法 將 GCJ-02 坐標轉換成 BD-09 坐標* * @param lng* @param lat* @return Gps*/public static Gps gcj02ToBd09(double lng, double lat) {double x = lng, y = lat;double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);double bd_lon = z * Math.cos(theta) + 0.0065;double bd_lat = z * Math.sin(theta) + 0.006;return new Gps(bd_lon, bd_lat);}/*** * 火星坐標系 (GCJ-02) 與百度坐標系 (BD-09) 的轉換算法 * * 將 BD-09 坐標轉換成GCJ-02 坐標 * ** * @param bd_lat* @param bd_lon* @return Gps* */public static Gps bd09ToGcj02(double lng, double lat) {double x = lng - 0.0065, y = lat - 0.006;double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);double gg_lon = z * Math.cos(theta);double gg_lat = z * Math.sin(theta);return new Gps(gg_lon, gg_lat);}/*** (BD-09)-->84* * @param lng* @param lat* @return Gps* */public static Gps bd09ToGps84(double lng, double lat) {Gps gcj02 = bd09ToGcj02(lng, lat);Gps map84 = gcj02ToGps84(gcj02.getLng(), gcj02.getLat());return map84;}/*** 是否在中國境內* * @param lng* @param lat* @return*/public static boolean outOfChina(double lng, double lat) {if (lng < 72.004 || lng > 137.8347)return true;if (lat < 0.8293 || lat > 55.8271)return true;return false;}public static Gps transform(double lng, double lat) {if (outOfChina(lng, lat)) {return new Gps(lng, lat);}double dLat = transformLat(lng - 105.0, lat - 35.0);double dLon = transformLng(lng - 105.0, lat - 35.0);double radLat = lat / 180.0 * PI;double magic = Math.sin(radLat);magic = 1 - EE * magic * magic;double sqrtMagic = Math.sqrt(magic);dLat = (dLat * 180.0) / ((R * (1 - EE)) / (magic * sqrtMagic) * PI);dLon = (dLon * 180.0) / (R / sqrtMagic * Math.cos(radLat) * PI);double mgLat = lat + dLat;double mgLon = lng + dLon;return new Gps(mgLon, mgLat);}public static double transformLat(double x, double y) {double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;return ret;}public static double transformLng(double x, double y) {double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;return ret;}/*** 計算兩個經緯度直接的距離* * @param lng1* @param lat1* @param lng2* @param lat2* @return*/public static double getDistance(double lat1,double lng1,double lat2,double lng2) {double distance = 0;Gps gps1 = gcj02ToGps84(lng1, lat1);Gps gps2 = gcj02ToGps84(lng2, lat2);lat1 = gps1.getLat();lat2 = gps2.getLat();lng1 = gps1.getLng();lng2 = gps2.getLng();try {distance = Math.round(R * 2* Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 * PI / 180 - lat2 * PI / 180) / 2), 2)+ Math.cos(lat1 * PI / 180) * Math.cos(lat2 * PI / 180)* Math.pow(Math.sin((lng1 * PI / 180 - lng2 * PI / 180) / 2), 2))));} catch (Exception e) {e.printStackTrace();}return distance;} }最近在整理一些資源工具,放在網站分享?http://tools.maqway.com
歡迎關注公眾號:麻雀唯伊 , 不定時更新資源文章,生活優惠,或許有你想看的
?
?
?
?
?
?
總結
- 上一篇: python实现windows ie代理
- 下一篇: Easy Connect无法连接的情况,