IOS CLLocationManager和中国地图偏移(China Shift)
来源:互联网 发布:剑与魔法翅膀升阶数据 编辑:程序博客网 时间:2024/05/21 09:44
CLLocationManager类的作用是监听GPS的位置消息,当用户坐标发生变化时,会调用下面的方法进行通知:
-(void) locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)newLocationfromLocation:(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 CLLocationManager和中国地图偏移(China Shift)
- IOS CLLocationManager和中国地图偏移(China Shift)
- CLLocationManager和中国地图偏移(China Shift)
- CLLocationManager和中国地图偏移(China Shift)
- CLLocationManager和中国地图偏移
- CLLocationManager和中国地图偏移
- 中国地图偏移问题
- iOS开发--CoreMotion和CLLocationManager教程
- 【代码笔记】iOS-中国地图
- 关于IOS定位和CLLocationManager 小知识点总结
- ios定位CLLocationManager
- ios CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS CLLocationManager定位
- iOS--CLLocationManager 定位
- CLLocationManager和UIMapView
- vue 引入echarts画中国地图 Map china not exists
- No result defined for action jsp_entity.SysLog and result success
- 动态设置不同的行字体颜色
- ubuntu 看迅雷电影的方法
- Android开机启动流程初探
- Linux汇编与C互相调用
- IOS CLLocationManager和中国地图偏移(China Shift)
- linux-2.6.26内核中ARM中断实现详解
- Matrix Chain Multiplication(栈的简单应用)
- shell 创建文件夹,并在文件夹中创建空文件
- 考研小结
- 输出流效率测试
- Android警告错误搜集
- PHP学习-----文件操作/上传和下载
- 打造心中的暗黑传奇1