iOS地圖定位偏差問(wèn)題解決(不同坐標(biāo)系轉(zhuǎn)化)

國(guó)際共識(shí):WGS84的坐標(biāo)系統(tǒng),以經(jīng)緯度的形式來(lái)表示地球平面上的某一個(gè)位置凝垛;

中國(guó):GCJ-02的坐標(biāo)系統(tǒng)。在我國(guó)蜓谋,出于國(guó)家安全考慮梦皮,國(guó)內(nèi)所有導(dǎo)航電子地圖必須使用國(guó)家測(cè)繪局制定的加密坐標(biāo)系統(tǒng),即將一個(gè)真實(shí)的經(jīng)緯度坐標(biāo)加密成一個(gè)不正確的經(jīng)緯度坐標(biāo)桃焕,稱(chēng)之為火星坐標(biāo)剑肯;

百度:BD-09的坐標(biāo)系統(tǒng),百度坐標(biāo)是在國(guó)測(cè)局制定的GCJ-02,對(duì)地理位置進(jìn)行首次加密的基礎(chǔ)上观堂,進(jìn)行了BD-09二次加密措施,更加保護(hù)了個(gè)人隱私让网。

下面直接上代碼,直接創(chuàng)建一個(gè)坐標(biāo)轉(zhuǎn)化類(lèi)师痕,用的時(shí)候?qū)⒍ㄎ坏降腃LLocationCoordinate2D溃睹,直接通過(guò)所定義的類(lèi)轉(zhuǎn)化一下,再用的時(shí)候胰坟,地圖定位偏差較大的問(wèn)題即可解決因篇。

創(chuàng)建一個(gè)坐標(biāo)轉(zhuǎn)化類(lèi)

.h文件

#import《Foundation/Foundation.h》

#import《CoreLocation/CoreLocation.h》

@interface KNLocationConverter : NSObject

/**

*? 判斷是否在中國(guó)

*/

+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;

/**

*? 將WGS-84轉(zhuǎn)為GCJ-02(火星坐標(biāo)):

*/

+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc;

/**

*? 將GCJ-02(火星坐標(biāo))轉(zhuǎn)為百度坐標(biāo):

*/

+(CLLocationCoordinate2D)transformFromGCJToBaidu:(CLLocationCoordinate2D)p;

/**

*? 將百度坐標(biāo)轉(zhuǎn)為GCJ-02(火星坐標(biāo)):

*/

+(CLLocationCoordinate2D)transformFromBaiduToGCJ:(CLLocationCoordinate2D)p;

/**

*? 將GCJ-02(火星坐標(biāo))轉(zhuǎn)為WGS-84:

*/

+(CLLocationCoordinate2D)transformFromGCJToWGS:(CLLocationCoordinate2D)p;

@end


.m文件

#import "TQLocationConverter.h"

#import《math.h》

static const double a = 6378245.0;

static const double ee = 0.00669342162296594323;

static const double pi = 3.14159265358979324;

static const double xPi = M_PI? * 3000.0 / 180.0;

@implementation KNLocationConverter

+(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];

long double radLat = wgsLoc.latitude / 180.0 * pi;

long double magic = sin(radLat);

magic = 1 - ee * magic * magic;

long 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;

}

+ (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(fabs(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(fabs(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;

}

+(CLLocationCoordinate2D)transformFromGCJToBaidu:(CLLocationCoordinate2D)p

{

long double z = sqrt(p.longitude * p.longitude + p.latitude * p.latitude) + 0.00002 * sqrt(p.latitude * pi);

long double theta = atan2(p.latitude, p.longitude) + 0.000003 * cos(p.longitude * pi);

CLLocationCoordinate2D geoPoint;

geoPoint.latitude? = (z * sin(theta) + 0.006);

geoPoint.longitude = (z * cos(theta) + 0.0065);

return geoPoint;

}

+(CLLocationCoordinate2D)transformFromBaiduToGCJ:(CLLocationCoordinate2D)p

{

double x = p.longitude - 0.0065, y = p.latitude - 0.006;

double z = sqrt(x * x + y * y) - 0.00002 * sin(y * xPi);

double theta = atan2(y, x) - 0.000003 * cos(x * xPi);

CLLocationCoordinate2D geoPoint;

geoPoint.latitude? = z * sin(theta);

geoPoint.longitude = z * cos(theta);

return geoPoint;

}

+(CLLocationCoordinate2D)transformFromGCJToWGS:(CLLocationCoordinate2D)p

{

double threshold = 0.00001;

// The boundary

double minLat = p.latitude - 0.5;

double maxLat = p.latitude + 0.5;

double minLng = p.longitude - 0.5;

double maxLng = p.longitude + 0.5;

double delta = 1;

int maxIteration = 30;

// Binary search

while(true)

{

CLLocationCoordinate2D leftBottom? = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = minLat,.longitude = minLng}];

CLLocationCoordinate2D rightBottom = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = minLat,.longitude = maxLng}];

CLLocationCoordinate2D leftUp? ? ? = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = maxLat,.longitude = minLng}];

CLLocationCoordinate2D midPoint? ? = [[self class] transformFromWGSToGCJ:(CLLocationCoordinate2D){.latitude = ((minLat + maxLat) / 2),.longitude = ((minLng + maxLng) / 2)}];

delta = fabs(midPoint.latitude - p.latitude) + fabs(midPoint.longitude - p.longitude);

if(maxIteration-- <= 0 || delta <= threshold)

{

return (CLLocationCoordinate2D){.latitude = ((minLat + maxLat) / 2),.longitude = ((minLng + maxLng) / 2)};

}

if(isContains(p, leftBottom, midPoint))

{

maxLat = (minLat + maxLat) / 2;

maxLng = (minLng + maxLng) / 2;

}

else if(isContains(p, rightBottom, midPoint))

{

maxLat = (minLat + maxLat) / 2;

minLng = (minLng + maxLng) / 2;

}

else if(isContains(p, leftUp, midPoint))

{

minLat = (minLat + maxLat) / 2;

maxLng = (minLng + maxLng) / 2;

}

else

{

minLat = (minLat + maxLat) / 2;

minLng = (minLng + maxLng) / 2;

}

}

}

static bool isContains(CLLocationCoordinate2D point, CLLocationCoordinate2D p1, CLLocationCoordinate2D p2)

{

return (point.latitude >= MIN(p1.latitude, p2.latitude) && point.latitude <= MAX(p1.latitude, p2.latitude)) && (point.longitude >= MIN(p1.longitude,p2.longitude) && point.longitude <= MAX(p1.longitude, p2.longitude));

}

/**

*? 判斷是不是在中國(guó)

*/

+(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;

}

@end

用的時(shí)候,直接把國(guó)際坐標(biāo)轉(zhuǎn)換成火星坐標(biāo)笔横,就可以直接顯示定位信息了



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末竞滓,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子吹缔,更是在濱河造成了極大的恐慌商佑,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件厢塘,死亡現(xiàn)場(chǎng)離奇詭異茶没,居然都是意外死亡肌幽,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén)礁叔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)牍颈,“玉大人迄薄,你說(shuō)我怎么就攤上這事琅关。” “怎么了讥蔽?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵涣易,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我冶伞,道長(zhǎng)新症,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任响禽,我火速辦了婚禮徒爹,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘芋类。我一直安慰自己隆嗅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布侯繁。 她就那樣靜靜地躺著胖喳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贮竟。 梳的紋絲不亂的頭發(fā)上丽焊,一...
    開(kāi)封第一講書(shū)人閱讀 49,031評(píng)論 1 285
  • 那天,我揣著相機(jī)與錄音咕别,去河邊找鬼技健。 笑死,一個(gè)胖子當(dāng)著我的面吹牛惰拱,可吹牛的內(nèi)容都是我干的凫乖。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼弓颈,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼帽芽!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起翔冀,我...
    開(kāi)封第一講書(shū)人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤导街,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后纤子,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體搬瑰,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡款票,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了泽论。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片艾少。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖翼悴,靈堂內(nèi)的尸體忽然破棺而出缚够,到底是詐尸還是另有隱情,我是刑警寧澤鹦赎,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布谍椅,位于F島的核電站,受9級(jí)特大地震影響古话,放射性物質(zhì)發(fā)生泄漏雏吭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一陪踩、第九天 我趴在偏房一處隱蔽的房頂上張望杖们。 院中可真熱鬧,春花似錦肩狂、人聲如沸摘完。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)描焰。三九已至,卻和暖如春栅螟,著一層夾襖步出監(jiān)牢的瞬間荆秦,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工力图, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留步绸,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓吃媒,卻偏偏與公主長(zhǎng)得像瓤介,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赘那,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容