IOS LocationManager定位國內(nèi)偏移,火星坐標(GCJ-02)解決方法

#import

#import?

@interfaceWGS84TOGCJ02:?NSObject

//判斷是否已經(jīng)超出中國范圍

+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;

//轉(zhuǎn)GCJ-02

+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc;

@end

在WGS84TOGCJ02.m文件中:

//??Copyright?(c)?2013年?swinglife.?All?rights?reserved.

//

#import?"WGS84TOGCJ02.h"

constdoublea?=6378245.0;

constdoubleee?=0.00669342162296594323;

constdoublepi?=3.14159265358979324;

@implementationWGS84TOGCJ02

+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc

{

CLLocationCoordinate2D?adjustLoc;

if([selfisLocationOutOfChina:wgsLoc]){

adjustLoc?=?wgsLoc;

}else{

doubleadjustLat?=?[selftransformLatWithX:wgsLoc.longitude-105.0withY:wgsLoc.latitude-35.0];

doubleadjustLon?=?[selftransformLonWithX:wgsLoc.longitude-105.0withY:wgsLoc.latitude-35.0];

doubleradLat?=?wgsLoc.latitude/180.0*?pi;

doublemagic?=?sin(radLat);

magic?=1-ee*magic*?magic;

doublesqrtMagic?=?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;

}

returnadjustLoc;

}

//判斷是不是在中國

+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location

{

if(location.longitude<72.004||?location.longitude>137.8347||?location.latitude<0.8293||?location.latitude>55.8271)

returnYES;

returnNO;

}

+(double)transformLatWithX:(double)xwithY:(double)y

{

doublelat?=?-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;

returnlat;

}

+(double)transformLonWithX:(double)xwithY:(double)y

{

doublelon?=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;

returnlon;

}

@end

-(void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations{

//得到newLocation

CLLocation*loc?=?[locationsobjectAtIndex:0];

//判斷是不是屬于國內(nèi)范圍

if(![WGS84TOGCJ02isLocationOutOfChina:[loccoordinate]])?{

//轉(zhuǎn)換后的coord

CLLocationCoordinate2D?coord?=?[WGS84TOGCJ02transformFromWGSToGCJ:[loccoordinate]];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容