zoukankan      html  css  js  c++  java
  • [IOS] CLLocationManager和中国地图偏移(China Shift)

    CLLocationManager类的作用是监听GPS的位置消息,当用户坐标发生变化时,会调用下面的方法进行通知:

    -(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那样进行丰富的配置,至少目前我还没找到。

  • 相关阅读:
    WslRegisterDistribution failed with error: 0x80370102
    vscode C/C++ 语法检查
    ADO.NET 一(概述)
    线程三(Mutex)
    线程二(Monitor)
    线程一(lock)
    interface Part4(接口中的多态)
    interface Part3(实现:显示和隐式)
    interface Part2(定义接口)
    interface Part1(接口详解)
  • 原文地址:https://www.cnblogs.com/every2003/p/2600434.html
Copyright © 2011-2022 走看看