CLLocation与mapkit(中国)的偏移处理办法add 修正偏差的处理另外办法,利用MKMapView *_mapView获取location消除误差;
2012-12-22 20:03
621 查看
CLLocationManager类的作用是监听GPS的位置消息,当用户坐标发生变化时,会调用下面的方法进行通知:
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
问题来了,当我们把newLocation中的坐标映射到MKMapView控件上时,会发现这个点跟本不是我们所在的位置,而是离我们百米左右的某个地方。
至于原因,。
那么,我们需要将原始的(真实的)位置坐标,转换为有中国特色的坐标。
对于这个问题,本文总结了两个处理办法:
1. 使用IOS的私有类MKLocationManager来计算。
这个做法是有风险的,苹果不允许私有模块被直接调用。换句话说,你的软件可能会被Deny。
因为是私有模块,我们需要声明这个类和我们要用到的函数,代码如下
在CLLocationManager的位置监听函数中,我们把newLocation(原始位置),转换为中国位置
这样,经转换后的newLocation,已经是中国的位置了。现在在映射到MKMapView上时,会显示正确的所在位置。
2. 打开MKMapView的showsUserLocation功能。
初始化MKMapView时,将属性showsUserLocation设置为YES,MKMapView会启动内置的位置监听服务,当用户位置变化时,调用delegate的回调函数:
这个方法不会用到IOS的私有类和函数,不会有被绝的风险。缺点可能是不能像CLLocationManager那样进行丰富的配置,至少目前我还没找到。
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
问题来了,当我们把newLocation中的坐标映射到MKMapView控件上时,会发现这个点跟本不是我们所在的位置,而是离我们百米左右的某个地方。
至于原因,。
那么,我们需要将原始的(真实的)位置坐标,转换为有中国特色的坐标。
对于这个问题,本文总结了两个处理办法:
1. 使用IOS的私有类MKLocationManager来计算。
这个做法是有风险的,苹果不允许私有模块被直接调用。换句话说,你的软件可能会被Deny。
因为是私有模块,我们需要声明这个类和我们要用到的函数,代码如下
@interface MKLocationManager + (id)sharedLocationManager; // 创建并获取MKLocationManager实例 - (BOOL)chinaShiftEnabled; // 判断IOS系统是否支持计算偏移 - (CLLocation*)_applyChinaLocationShift:(CLLocation*)arg; // 传入原始位置,计算偏移后的位置 @end
在CLLocationManager的位置监听函数中,我们把newLocation(原始位置),转换为中国位置
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if ([[MKLocationManager sharedLocationManager] chinaShiftEnabled]) { newLocation = [[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation]; if (newLocation == nil) { // 很重要,计算location好像是要联网的,软件刚启动时前几次计算会返回nil。 return; } } ... }
这样,经转换后的newLocation,已经是中国的位置了。现在在映射到MKMapView上时,会显示正确的所在位置。
2. 打开MKMapView的showsUserLocation功能。
初始化MKMapView时,将属性showsUserLocation设置为YES,MKMapView会启动内置的位置监听服务,当用户位置变化时,调用delegate的回调函数:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { // 这里得到的userLocation,已经是偏移后的位置了 }
这个方法不会用到IOS的私有类和函数,不会有被绝的风险。缺点可能是不能像CLLocationManager那样进行丰富的配置,至少目前我还没找到。
相关文章推荐
- iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决办法
- iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决办法
- iOS 8中CLLocationManager及MKMapView showUserLocation失败的解决的方法
- 在用MKMapView self.mapView.showsUserLocation = YES; 实现定位时不能打开飞行模式
- iphone获取 用户坐标详细信息 MKPlacemark userLocation MKMapView
- Android 获取服务器数据解析Text数据,存在htmel标签的处理办法
- View在属性为wrap_content/match_parent时获取宽高不准确的解决办法
- 把一个用sql server 2005内置加密方法加密数据的DB从一台电脑备份后,还原到另外一台电脑上,出现的问题的处理办法
- ios中点击地图控件MKMapView的某点获取该点的经纬度
- Windows环境下利用“共享内存”实现进程间通信的C/C++代码---利用CreateFileMapping和MapViewOfFile
- C# 利用HttpWebRequest模拟登陆获取数据设置Accept-Encoding为gzip,deflate后返回的网页是乱码处理
- InSAR-DInSAR 技术细节(七)轨道误差及其处理办法
- 网络获取图片数据后 利用viewpager实现弹簧效果
- 利用CMHTMLView实现webview基础功能,同时获取点击图片链接的功能
- ios利用MKMapView实现简单的地图
- Android 利用addView 动态给Activity添加View组件
- UITableView获取滚动的偏移
- (转) 在C#中使用WIA获取扫描仪数据(三、利用Filter处理图片)
- Android TV RecyclerView 焦点处理及获取焦点的Item保持在中间
- git gui 无法获取你的身份 处理办法