ios定位

来源:互联网 发布:电脑动画制作软件 编辑:程序博客网 时间:2024/04/30 03:16

转载:

文章一 http://blog.csdn.net/xiaobaismiley/article/details/37576303

文章二:http://blog.csdn.net/swingpyzf/article/details/16972351


文章一:

     标准地球坐标即GPS设备获得的坐标,该坐标需要经过国家测绘局进行加密后形成火星坐标GCJ-02,国外坐标(WGS-84 )我们用的google坐标和高德地图坐标也就是火星坐标

         百度地图,在火星坐标的基础上再进行一次加密,形成了百度地图上的坐标,因此,直接将标准地球坐标显示在百度地图上是会有几百米的偏差的。按照此原理,标准GPS坐标经过两步的转换可得到百度坐标。因为在处理百度地图时,例如查询其POI都需要百度地图上的坐标,而不是标准坐标,那么这样的转换就是必要的了。下面是两步转换的C++ 程序,供开发的人做一个参考。

[cpp] view plain copy
  1. //============================================================================  
  2. // Name        : Convert_cpp.cpp  
  3. // Author      : roger  
  4. // Version     :  
  5. // Copyright   : Your copyright notice  
  6. // Description : Hello World in C++, Ansi-style  
  7. //============================================================================  
  8.   
  9. #include <iostream>  
  10. #include <math.h>  
  11. #include <stdlib.h>  
  12. #include <iomanip>  
  13. using namespace std;  
  14.   
  15. const double pi = 3.14159265358979324;  
  16.     const double a = 6378245.0;  
  17.     const double ee = 0.00669342162296594323;  
  18.     const  double x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
  19.   
  20.     bool outOfChina(double lat, double lon)  
  21.     {  
  22.         if (lon < 72.004 || lon > 137.8347)  
  23.             return true;  
  24.         if (lat < 0.8293 || lat > 55.8271)  
  25.             return true;  
  26.         return false;  
  27.     }  
  28.   
  29.      double transformLat(double x, double y)  
  30.     {  
  31.         double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));  
  32.         ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;  
  33.         ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;  
  34.         ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;  
  35.         return ret;  
  36.     }  
  37.   
  38.      double transformLon(double x, double y)  
  39.     {  
  40.         double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));  
  41.         ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;  
  42.         ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;  
  43.         ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;  
  44.         return ret;  
  45.     }  
  46.   
  47.     /** 
  48.      * 地球坐标转换为火星坐标 
  49.      * World Geodetic System ==> Mars Geodetic System 
  50.      * 
  51.      * @param wgLat  地球坐标 
  52.      * @param wgLon 
  53.      * 
  54.      * mglat,mglon 火星坐标 
  55.      */  
  56.      void transform2Mars(double wgLat, double wgLon,double &mgLat,double &mgLon)  
  57.     {  
  58.         if (outOfChina(wgLat, wgLon))  
  59.         {  
  60.             mgLat  = wgLat;  
  61.             mgLon = wgLon;  
  62.             return ;  
  63.         }  
  64.         double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);  
  65.         double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);  
  66.         double radLat = wgLat / 180.0 * pi;  
  67.         double magic = sin(radLat);  
  68.         magic = 1 - ee * magic * magic;  
  69.         double sqrtMagic = sqrt(magic);  
  70.         dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);  
  71.         dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);  
  72.         mgLat = wgLat + dLat;  
  73.         mgLon = wgLon + dLon;  
  74.   
  75.     }  
  76.   
  77.     /** 
  78.      * 火星坐标转换为百度坐标 
  79.      * @param gg_lat 
  80.      * @param gg_lon 
  81.      */  
  82.      void bd_encrypt(double gg_lat, double gg_lon,double &bd_lat,double & bd_lon)  
  83.     {  
  84.         double x = gg_lon, y = gg_lat;  
  85.         double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);  
  86.         double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);  
  87.         bd_lon = z * cos(theta) + 0.0065;  
  88.         bd_lat = z * sin(theta) + 0.006;  
  89.   
  90.     }  
  91.   
  92.     /** 
  93.      * 百度转火星 
  94.      * @param bd_lat 
  95.      * @param bd_lon 
  96.      */  
  97.      void bd_decrypt(double bd_lat, double bd_lon,double &gg_lat,double &gg_lon)  
  98.     {  
  99.         double x = bd_lon - 0.0065, y = bd_lat - 0.006;  
  100.         double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);  
  101.         double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);  
  102.         gg_lon = z * cos(theta);  
  103.         gg_lat = z * sin(theta);  
  104.   
  105.     }  
  106.   
  107.   
  108.   
  109. int main() {  
  110.     double lat = 30.227607;  
  111.     double lon = 120.036565;  
  112.   
  113.     //真实的经纬度转化为百度地图上的经纬度,便于计算百度POI  
  114.     double marsLat = 0;  
  115.     double marsLon = 0;  
  116.     double resultLat = 0;  
  117.     double resultLon = 0;  
  118.     transform2Mars(lat,lon,marsLat,marsLon);  
  119.     bd_encrypt(marsLat,marsLon,resultLat,resultLon);  
  120.   
  121.     //30.2193456 120.0348264  
  122.     cout<<setprecision(10)<<resultLat<<" "<<setprecision(10)<<resultLon<<endl;  
  123.   
  124. }  
         在自己的项目中,将标准GPS转换为百度坐标后,再获取百度地图的POI,获得的POI位置和真实的位置基本一致,可以使用。下面还有一些网上转载的资料,比较丰富,值得一看。

-----------------------------------------------------------------------------------------------------

  1. GCJ-02坐标系统(火星坐标)简介:http://blog.csdn.net/giswens/article/details/8775121(存档:http://mapbd.com/cms/2012/07/25/)
  2. GCJ-02到真实坐标反向变换的理论基础:http://blog.csdn.net/giswens/article/details/8775213
  3. WGS84坐标与Web墨卡托坐标互转:http://blog.csdn.net/giswens/article/details/9634261
  4. 地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法:
    • C#代码:https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936(注解:http://blog.csdn.net/giswens/article/details/8775283)
    • Java代码:http://emq.googlecode.com/svn/emq/src/Algorithm/Coords/Converter.java
    • iOS代码:http://blog.csdn.net/giswens/article/details/8775183(存档:http://www.keakon.net/2011/07/02/WGS84坐标转火星坐标(iOS篇))
  5. 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法:http://blog.csdn.net/coolypf/article/details/8569813(解释:国际经纬度坐标标准为WGS-84,国内必须至少使用国测局制定的GCJ-02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施,更加保护了个人隐私。百度对外接口的坐标系并不是GPS采集的真实经纬度,需要通过坐标转换接口进行转换。)
  6. 一种根据纠偏数据对火星坐标进行完美拟合的方法:http://blog.sina.com.cn/s/blog_538036cf0100pxbl.html
  7. 国内各地图API坐标系统比较:http://rovertang.com/archives/547
  8. 关于百度地图坐标转换接口的研究:http://rovertang.com/archives/24699


查询过资料可得,通过程序进行 标准坐标转火星坐标(google、高德),火星坐标转百度坐标,百度坐标转火星坐标,百度坐标转标准坐标都是可行的,火星直接转标准坐标转不了。




文章二:

CLLocationManager类可以实时的获得我们位置的经纬度,并且可以通过经纬度在MapView上定位:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  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上

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  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中对自身定位来获得:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //定义一个MKMapView 并且调用setShowUserLocation:YES来获得自身的位置  
  2. [self.mapView setShowsUserLocation:YES];  
  3.   
  4. //如果不想要显示这个MKMapView就将其隐藏  
  5. [self.mapView setHidden:YES];  

然后通过MKMapView的委托方法来获取准确的经纬度:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{  
  2. CLLocationCoordinate2D coord = [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头文件中定义:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  2. //  
  3.   
  4. #import <Foundation/Foundation.h>  
  5. #import <MapKit/MapKit.h>  
  6.   
  7. @interface WGS84TOGCJ02 : NSObject  
  8. //判断是否已经超出中国范围  
  9. +(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;  
  10. //转GCJ-02  
  11. +(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc;  
  12. @end  
在WGS84TOGCJ02.m文件中:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  Copyright (c) 2013年 swinglife. All rights reserved.  
  2. //  
  3.   
  4. #import "WGS84TOGCJ02.h"  
  5.   
  6. const double a = 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.     CLLocationCoordinate2D adjustLoc;  
  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 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 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 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 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结构类型

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  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.         CLLocationCoordinate2D coord = [WGS84TOGCJ02 transformFromWGSToGCJ:[loc coordinate]];  
  9.     }  


0 0
原创粉丝点击