IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法

#import

#import 

@interfaceWGS84TOGCJ02: NSObject

//判断是否已经超出中国范围

+(BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location;

//转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];

//判断是不是属于国内范围

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

//转换后的coord

CLLocationCoordinate2D coord = [WGS84TOGCJ02transformFromWGSToGCJ:[loccoordinate]];

}

你可能感兴趣的:(IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法)