【IOS開發】高德地圖定位座標偏差

關於地圖相當重要的一篇文章:http://www.cnblogs.com/kenshincui/p/4125570.html
原文地址:http://blog.csdn.net/swingpyzf/article/details/16972351

CLLocationManager類可以實時的獲得我們位置的經緯度,並且可以通過經緯度在MapView上定位:

 

  1. //創建CLLocationManager對象  
  2.    CLLocationManager *locationManager [[CLLocationManager alloc] init];  
  3.      
  4.    //設置委託對象爲自己  
  5.    [locationManager setDelegate:self];  
  6.      
  7.    //要求CLLocationManager對象返回全部結果  
  8.    [locationManager setDistanceFilter:kCLDistanceFilterNone];  
  9.      
  10.    //要求CLLocationManager對象的返回結果儘可能的精準  
  11.    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];  
  12.      
  13.    //要求CLLocationManager對象開始工作,定位設備位置  
  14.    [locationManager startUpdatingLocation];  

通過下面的CLLocationManager的委託方法可以得到或者更新locationManager的經緯度,並且顯示到MapView上

 

 

  1. //CLLocationManager委託方法  
  2. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{  
  3.     //得到newLocation  
  4.     CLLocation *loc [locations objectAtIndex:0];  
  5.  

但是對於國內地圖而言,使用LocationManager定位所獲得經緯度,是有一段較大距離的偏移的。

這是爲什麼呢??這幾天一直在查這方面的資料,各種google、baidu論壇裏給出的答案也各有不同:
wifi 熱點 基站 gps等等所導致的都有,最後查找還是找到了問題所在,原來國內地圖使用的座標系統是GCJ-02而ios sdk中所用到的是國際標準的座標系統WGS-84

因爲國內使用的是加密後的座標系GCJ-02就是網絡上叫的火星座標。

 

locationManager就是因爲得到的是火星座標偏移後的經緯度,所以導致在MapView上有很大的偏差,而在MKMapView上通過定位自己位置所獲得的經緯度有是準確,因爲apple已經對國內地圖做了偏移優化。


1、那麼臨時的解決方法:想要獲得自己準確的經緯度可以直接通過MKMapView中對自身定位來獲得:

 

  1. //定義一個MKMapView 並且調用setShowUserLocation:YES來獲得自身的位置  
  2. [self.mapView setShowsUserLocation:YES];  
  3.   
  4. //如果不想要顯示這個MKMapView就將其隱藏  
  5. [self.mapView setHidden:YES];  

 

然後通過MKMapView的委託方法來獲取準確的經緯度:

  1. -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{  
  2. CLLocationCoordinate2coord [userLocation coordinate];  
  3. NSLog(@"經度:%f,緯度:%f",coord.latitude,coord.longitude);  
  4.  

這個方法雖然簡單,也可以實時的獲取自己的位置而且基本上都是準確的位置,但是也就無法使用locationManager中的一些功能了。

那麼有沒有更好的解方法可以讓我們在火星座標上使用locationManager來獲得準確的位置呢? 看了幾篇博客給出的答案是相似的,就是調用apple的私有模塊類中得方法來對經緯度做一個偏移修正:

http://blog.csdn.net/zhibudefeng/article/details/8495837

http://yach.me/blog/2011/11/09/huo-xing-zuo-biao-xi-wen-ti-zai-iosxia-de-xiu/

上面兩篇文章中都提到了使用apple的私有模塊MKLocationManager來修正偏移,不過遺憾的是如果使用了私有模塊會導致AppStore審覈失敗,但是幸運的是這種方式只能在IOS5以前的系統中使用。。。。。。至少今天我們不用擔心該不該使用這個方式。

接下來就是正題了:最後我找到了一份android的火星座標轉換的算法,然後自己改寫成了以下將WGS-84座標系統轉爲GCJ-02的Objective-C代碼,這樣就能方便的使用CLLocationManager這個類了。

2、將WGS-84轉爲GCJ-02(火星座標):

新建一個類“WGS84TOGCJ02”,在.h頭文件中定義:

 

  1. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  2. //  
  3.   
  4. #import   
  5. #import   
  6.   
  7. @interface WGS84TOGCJ02 NSObject  
  8. //判斷是否已經超出中國範圍  
  9. +(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;  
  10. //轉GCJ-02  
  11. +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc;  
  12. @end  
在WGS84TOGCJ02.m文件中:

 

 

  1. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  2. //  
  3.   
  4. #import "WGS84TOGCJ02.h"  
  5.   
  6. const double 6378245.0 
  7. const double ee 0.00669342162296594323 
  8. const double pi 3.14159265358979324 
  9.   
  10. @implementation WGS84TOGCJ02  
  11.   
  12. +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc  
  13.  
  14.     CLLocationCoordinate2adjustLoc;  
  15.     if([self isLocationOutOfChina:wgsLoc]){  
  16.         adjustLoc wgsLoc;  
  17.     }else 
  18.         double adjustLat [self transformLatWithX:wgsLoc.longitude 105.0 withY:wgsLoc.latitude 35.0];  
  19.         double adjustLon [self transformLonWithX:wgsLoc.longitude 105.0 withY:wgsLoc.latitude 35.0];  
  20.         double radLat wgsLoc.latitude 180.0 pi;  
  21.         double magic sin(radLat);  
  22.         magic 1 ee magic magic;  
  23.         double sqrtMagic sqrt(magic);  
  24.         adjustLat (adjustLat 180.0((a (1 ee)) (magic sqrtMagic) pi);  
  25.         adjustLon (adjustLon 180.0(a sqrtMagic cos(radLat) pi);  
  26.         adjustLoc.latitude wgsLoc.latitude adjustLat;  
  27.         adjustLoc.longitude wgsLoc.longitude adjustLon;  
  28.      
  29.     return adjustLoc;  
  30.  
  31.   
  32. //判斷是不是在中國  
  33. +(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location  
  34.  
  35.     if (location.longitude 72.004 || location.longitude 137.8347 || location.latitude 0.8293 || location.latitude 55.8271 
  36.         return YES 
  37.     return NO 
  38.  
  39.   
  40. +(double)transformLatWithX:(double)x withY:(double)y  
  41.  
  42.     double lat -100.0 2.0 3.0 0.2 y 0.1 x 0.2 sqrt(abs(x));  
  43.     lat += (20.0 sin(6.0 x pi) 20.0 *sin(2.0 x pi)) 2.0 3.0 
  44.     lat += (20.0 sin(y pi) 40.0 sin(y 3.0 pi)) 2.0 3.0 
  45.     lat += (160.0 sin(y 12.0 pi) 3320 sin(y pi 30.0)) 2.0 3.0 
  46.     return lat;  
  47.  
  48.   
  49. +(double)transformLonWithX:(double)x withY:(double)y  
  50.  
  51.     double lon 300.0 2.0 0.1 x 0.1 x 0.1 sqrt(abs(x));  
  52.     lon += (20.0 sin(6.0 x pi) 20.0 sin(2.0 x pi)) 2.0 3.0 
  53.     lon += (20.0 sin(x pi) 40.0 sin(x 3.0 pi)) 2.0 3.0 
  54.     lon += (150.0 sin(x 12.0 pi) 300.0 sin(x 30.0 pi)) 2.0 3.0 
  55.     return lon;  
  56.  
  57.   
  58. @end  

最後我們通過判斷isLocationOutOfChina 然後調用transformLatWithX方法就能獲取轉換後的 CLLocationCoordinate2D結構類型

 

 

  1. //CLLocationManager委託方法  
  2. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{  
  3.     //得到newLocation  
  4.     CLLocation *loc [locations objectAtIndex:0];  
  5.     //判斷是不是屬於國內範圍  
  6.     if (![WGS84TOGCJ02 isLocationOutOfChina:[loc coordinate]])  
  7.         //轉換後的coord  
  8.         CLLocationCoordinate2coord [WGS84TOGCJ02 transformFromWGSToGCJ:[loc coordinate]];  
  9.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章