您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS LBS 詳解(一):定位與中國特色的座標偏移糾正

編輯:iOS 相關資訊

談到 LBS(位置服務),第一步自然是定位——取得使用者目前所在的位置。iOS 中最常用的定位手段就是 CLLocationManager。而在中國做定位開發時,還有一個繞不開的課題:座標系偏移的糾正。本文就從定位講起,再說明如何處理這個「中國特色」的問題。

一、基礎定位:CLLocationManager

使用步驟很簡單:建立一個 CLLocationManager 實例,設定好 delegate,然後呼叫 startUpdatingLocation 即可開始定位。過程中還可以透過 CLLocationAccuracy 指定定位精度。

設定 delegate 之後,有兩個關鍵的回調方法:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;

顧名思義,第一個方法在定位成功時觸發,可以從 newLocation 中取出目前的經緯度(coordinate);第二個方法則在定位失敗時觸發。

還有一點值得注意:第一次定位時,系統會彈出隱私提示框,詢問使用者是否允許存取位置資訊。若使用者尚未點選,這兩個回調方法都不會被觸發,直到使用者按下「允許」或「禁止」後,才會分別回調成功或失敗。

實務上有個小建議:由於首次定位的結果往往不太準確,可以在短時間內定位幾次,取平均值以提高精度。

二、中國特色的座標糾偏

由於歷史與法規因素,在中國境內使用 GPS 取得的 WGS-84 座標,與實際地圖上的位置會存在偏移。地圖服務普遍採用的是 GCJ-02(俗稱「火星座標」)座標系。因此,若要把定位結果正確地標示在地圖上,就需要把世界標準的 WGS-84 座標轉換為 GCJ-02 座標。

處理思路是:先判斷座標是否落在中國境內,若在中國才需要轉換,否則直接返回原始經緯度。以下是核心程式碼:

// WGS-84 轉 GCJ-02
+ (CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
    CLLocationCoordinate2D adjustLoc;
    if ([self isLocationOutOfChina:wgsLoc]) {
        adjustLoc = wgsLoc;   // 不在中國,直接返回
    } else {
        double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
        double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
        double radLat = wgsLoc.latitude / 180.0 * pi;
        double magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = sqrt(magic);
        adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        adjustLoc.latitude = wgsLoc.latitude + adjustLat;
        adjustLoc.longitude = wgsLoc.longitude + adjustLon;
    }
    return adjustLoc;
}

// 判斷座標是否在中國境外
+ (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location
{
    if (location.longitude < 72.004 || location.longitude > 137.8347 ||
        location.latitude < 0.8293 || location.latitude > 55.8271) {
        return YES;
    }
    return NO;
}

+ (double)transformLatWithX:(double)x withY:(double)y
{
    double lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
    lat += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
    lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
    return lat;
}

+ (double)transformLonWithX:(double)x withY:(double)y
{
    double lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
    lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
    lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
    lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
    return lon;
}

其中 a 與 ee 是與地球橢球體相關的常數(長半軸與第一偏心率平方)。isLocationOutOfChina 透過經緯度範圍快速判斷座標是否落在中國境外,若境外則跳過轉換,直接使用原座標即可。這樣一來,就能讓定位結果在中國地圖上正確對位。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved