iOS開發中的火星坐標系及各種坐標系轉換算法


原文地址:http://m.oschina.net/blog/619183?ref=myread

 

 

其原理是這樣的:保密局開發了一個系統,能將實際的坐標轉換成虛擬的坐標。所有在中國銷售的數字地圖必須使用這個系統進行坐標轉換之后方可上市。這是生產環節,這種電子地圖被稱為火星地圖。在使用環節,GPS終端設備必須集成保密局提供的加密算法(集成工作由保密局完成),把從GPS衛星那里得到的坐標轉換成虛擬坐標,然后再去火星地圖上查找,這樣就在火星坐標系上完成了地圖的匹配。 所以大家所用的百度,高德等地圖定位准是偏差幾百米

名詞總結:

地球坐標:指WGS84坐標系統

火星坐標:指使用國家保密插件人為偏移后的坐標
地球地圖:指與地球坐標對應的客觀真實的地圖
火星地圖:指經過加密偏移后的,與火星坐標對應的地圖

坐標系轉換算法

1.GCJ-02(火星坐標系)和BD-09轉換

[cpp]  view plain  copy
 
  1. // GCJ-02 坐標轉換成 BD-09 坐標  
  2. + (CLLocationCoordinate2D)MarsGS2BaiduGS:(CLLocationCoordinate2D)coordinate  
  3. {  
  4.     double x_pi = PI * 3000.0 / 180.0;  
  5.     double x = coordinate.longitude, y = coordinate.latitude;  
  6.     double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);  
  7.     double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);  
  8.     double bd_lon = z * cos(theta) + 0.0065;  
  9.     double bd_lat = z * sin(theta) + 0.006;  
  10.     return CLLocationCoordinate2DMake(bd_lat, bd_lon);  
  11. }  
  12.   
  13. // BD-09 坐標轉換成 GCJ-02 坐標  
  14. + (CLLocationCoordinate2D)BaiduGS2MarsGS:(CLLocationCoordinate2D)coordinate  
  15. {  
  16.     double x_pi = PI * 3000.0 / 180.0;  
  17.     double x = coordinate.longitude - 0.0065, y = coordinate.latitude - 0.006;  
  18.     double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);  
  19.     double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);  
  20.     double gg_lon = z * cos(theta);  
  21.     double gg_lat = z * sin(theta);  
  22.     return CLLocationCoordinate2DMake(gg_lat, gg_lon);  
  23. }  

2WGS-84(地球坐標系)和BD-09(百度坐標)轉換

 

[cpp]  view plain  copy
 
  1. // WGS-84 坐標轉換成 BD-09 坐標  
  2. + (CLLocationCoordinate2D)WorldGS2BaiduGS:(CLLocationCoordinate2D)coordinate  
  3. {  
  4.     CLLocationCoordinate2D mars = [ALDGeocoder WorldGS2MarsGS:coordinate];  
  5.     CLLocationCoordinate2D baidu = [ALDGeocoder MarsGS2BaiduGS:mars];  
  6.     return baidu;  
  7. }  
  8.   
  9. // BD-09 坐標轉換成 WGS-84 坐標  
  10. + (CLLocationCoordinate2D)BaiduGS2WorldGS:(CLLocationCoordinate2D)coordinate  
  11. {  
  12.     CLLocationCoordinate2D mars = [ALDGeocoder BaiduGS2MarsGS:coordinate];  
  13.     CLLocationCoordinate2D world = [ALDGeocoder MarsGS2WorldGS:mars];  
  14.     return world;  
  15. }  

3.WGS-84和sogou坐標轉換

[cpp]  view plain  copy
 
  1. // WGS-84 坐標轉換成 Sogou 坐標  
  2. + (CLLocationCoordinate2D)WorldGS2SogouGS:(CLLocationCoordinate2D)coordinate  
  3. {  
  4.     const double ee = 0.082271854224939184;  
  5.     double lon = coordinate.longitude;  
  6.     double lat = coordinate.latitude;  
  7.     double dlon = [ALDGeocoder rad:CLIP(lon, -360, 360)];  
  8.     double dlat = [ALDGeocoder rad:CLIP(lat, -90, 90)];  
  9.     dlon = 6378206.4 * dlon;  
  10.     double sinphi = sin(dlat);  
  11.     double temp1, temp2;  
  12.     if((temp1 = 1.0 + sinphi) == 0.0){  
  13.         dlat = -1000000000;  
  14.     }else if((temp2 = 1.0 - sinphi) == 0.0){  
  15.         dlat = 1000000000;  
  16.     }else{  
  17.         double esinphi = ee * sinphi;  
  18.         dlat = 3189103.2000000002 * log((temp1 / temp2) * pow((1.0 - esinphi) / (1.0 + esinphi), ee));  
  19.     }  
  20.     return CLLocationCoordinate2DMake(dlat, dlon);  
  21. }  
  22.   
  23. // Sogou 坐標轉換成 WGS-84 坐標  
  24. + (CLLocationCoordinate2D)SogouGS2WorldGS:(CLLocationCoordinate2D)coordinate  
  25. {  
  26.     const double ee = 1.5707963267948966;  
  27.     const double aa = 0.0033938814110493522;  
  28.     double lon = coordinate.longitude;  
  29.     double lat = coordinate.latitude;  
  30.     double dlon = lon / 6378206.4;  
  31.     double temp = -lat / 6378206.4;  
  32.     double chi;  
  33.     if(temp < -307){  
  34.         chi = ee;  
  35.     }else if(temp > 308){  
  36.         chi = -ee;  
  37.     }else{  
  38.         chi = ee - 2 * atan(exp(temp));  
  39.     }  
  40.     double chi2 = 2 * chi;  
  41.     double coschi2 = cos(chi2);  
  42.     double dlat = chi + sin(chi2) * (aa + coschi2 * (1.3437644537757259E-005 + coschi2 * (7.2964865099246009E-008 + coschi2 * 4.4551470401894685E-010)));  
  43.     double rlon = CLIP([ALDGeocoder deg:dlon], -360, 360);  
  44.     double rlat = CLIP([ALDGeocoder deg:dlat], -90, 90);  
  45.     return CLLocationCoordinate2DMake(rlat, rlon);  
  46. }  

4火星坐標和地球坐標轉換

[cpp]  view plain  copy
 
  1. // World Geodetic System ==> Mars Geodetic System  
  2. + (CLLocationCoordinate2D)WorldGS2MarsGS:(CLLocationCoordinate2D)coordinate  
  3. {  
  4.     // a = 6378245.0, 1/f = 298.3  
  5.     // b = a * (1 - f)  
  6.     // ee = (a^2 - b^2) / a^2;  
  7.     const double a = 6378245.0;  
  8.     const double ee = 0.00669342162296594323;  
  9.       
  10.     if (outOfChina(coordinate.latitude, coordinate.longitude))  
  11.     {  
  12.         return coordinate;  
  13.     }  
  14.     double wgLat = coordinate.latitude;  
  15.     double wgLon = coordinate.longitude;  
  16.     double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);  
  17.     double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);  
  18.     double radLat = wgLat / 180.0 * PI;  
  19.     double magic = sin(radLat);  
  20.     magic = 1 - ee * magic * magic;  
  21.     double sqrtMagic = sqrt(magic);  
  22.     dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);  
  23.     dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * PI);  
  24.       
  25.     return CLLocationCoordinate2DMake(wgLat + dLat, wgLon + dLon);  
  26. }  
  27.   
  28. // Mars Geodetic System ==> World Geodetic System  
  29. + (CLLocationCoordinate2D)MarsGS2WorldGS:(CLLocationCoordinate2D)coordinate  
  30. {  
  31.     double gLat = coordinate.latitude;  
  32.     double gLon = coordinate.longitude;  
  33.     CLLocationCoordinate2D marsCoor = [ALDGeocoder WorldGS2MarsGS:coordinate];  
  34.     double dLat = marsCoor.latitude - gLat;  
  35.     double dLon = marsCoor.longitude - gLon;  
  36.     return CLLocationCoordinate2DMake(gLat - dLat, gLon - dLon);  
  37. }  

 

5WGS-84 和 墨卡托 坐標轉換

[cpp]  view plain  copy
 
  1. //WGS-84 坐標轉換成 墨卡托 坐標  
  2. + (CLLocationCoordinate2D)WorldGS2Mercator:(CLLocationCoordinate2D)coordinate  
  3. {  
  4.     double lon = coordinate.longitude*20037508.34/180;  
  5.     double lat = log(tan((90+coordinate.latitude)*M_PI/360))/(M_PI/180);  
  6.     lat = lat*20037508.34/180;  
  7.     return CLLocationCoordinate2DMake(lat, lon);  
  8. }  
  9.   
  10. //墨卡托 坐標轉換成 WGS-84 坐標  
  11. + (CLLocationCoordinate2D)Mercator2WorldGS:(CLLocationCoordinate2D)mercator  
  12. {  
  13.     double lon = mercator.longitude/20037508.34*180;  
  14.     double lat = mercator.latitude/20037508.34*180;  
  15.     lat = 180/M_PI*(2*atan(exp(lat*M_PI/180))-M_PI/2);  
  16.     return CLLocationCoordinate2DMake(lat, lon);  
  17. }  

 

開發時所面臨的現狀

獲取經緯度(GPS)

  • 火星坐標

    • MKMapView

  • 地球坐標

    • CLLocationManager

顯示經緯度(地圖)

  • 火星坐標

    • iOS 地圖

    • Gogole地圖

    • 搜搜、阿里雲、高德地圖

  • 地球坐標

    • Google 衛星地圖(國外地圖應該都是……)

  • 百度坐標

    • 百度地圖

推薦的解決方案:

    • 既然是在國內,存儲一律用火星坐標,這樣在使用國內地圖顯示時最方便(用百度地圖顯示時可以一次轉換取得)

    • CLLocationManager 拿到的 CLLocation 轉為火星坐標,MKMapView 不用處理

    • 使用地圖 API 進行 地址解析/逆地址解析(Geocoding) 時注意相應使用相應地圖商的坐標系

    • 部分地圖商支持多個坐標系輸入,如高德支持地球、火星坐標(這個一直有變動,具體只能參考廠商最新文檔了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM