WGS84,全稱World Geodetic System 1984,是為GPS全球定位系統使用而建立的坐標系統。
通常使用百度地图时需要进行坐标转换,遇到这个问题后进行了这方面的研究,目前自己能够使用的有三种方法,进行了一下总结,希望能帮到有需要的人。
1.百度SDK 转换
通过下载百度地图的SDK包后直接进行转换:
// coor为原GPS坐标
CLLocationCoordinate2D test=CLLocationCoordinate2DMake(coor.latitude, coor.longitude);
NSDictionary *testdic =BMKConvertBaiduCoorFrom(test,BMK_COORDTYPE_GPS);
//转换GPS坐标至百度坐标
NSString *xstr=[testdicobjectForKey:@"x"];
NSString *ystr=[testdicobjectForKey:@"y"];
NSData *xdata=[[NSDataalloc] initWithBase64EncodedString:xstroptions:0];
NSData *ydata=[[NSDataalloc] initWithBase64EncodedString:ystroptions:0];
NSString *xlat=[[NSStringalloc] initWithData:ydataencoding:NSUTF8StringEncoding];
NSString *ylng=[[NSStringalloc] initWithData:xdataencoding:NSUTF8StringEncoding];
coor.latitude=[xlat doubleValue];
coor.longitude=[ylng doubleValue];
//coor 现在为已转换的百度坐标
2.通过函数计算 转换
coor=[selfwgs84ToBd09:coor];
函数直接转换 (也可以写工具类放入其中进行调用)
-(CLLocationCoordinate2D)wgs84ToBd09:(CLLocationCoordinate2D)location
{
CLLocationCoordinate2D gcj02Pt = [selfgcj02Encrypt:location.latitude
bdLon:location.longitude];
return [selfbd09Encrypt:gcj02Pt.latitudebdLon:gcj02Pt.longitude] ;
}
-(CLLocationCoordinate2D)wgs84ToGcj02:(CLLocationCoordinate2D)location
{
return [selfgcj02Encrypt:location.latitudebdLon:location.longitude];
}
-(CLLocationCoordinate2D)gcj02Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D resPoint;
double mgLat;
double mgLon;
if ([selfoutOfChina:ggLat bdLon:ggLon]) {
resPoint.latitude = ggLat;
resPoint.longitude = ggLon;
return resPoint;
}
double dLat = [selftransformLat:(ggLon - 105.0)bdLon:(ggLat -35.0)];
double dLon = [selftransformLon:(ggLon - 105.0)bdLon:(ggLat - 35.0)];
double radLat = ggLat /180.0 * M_PI;
double magic =sin(radLat);
magic = 1 -jzEE * magic * magic;
double sqrtMagic =sqrt(magic);
dLat = (dLat * 180.0) / ((jzA * (1 -jzEE)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (jzA / sqrtMagic *cos(radLat) * M_PI);
mgLat = ggLat + dLat;
mgLon = ggLon + dLon;
resPoint.latitude = mgLat;
resPoint.longitude = mgLon;
return resPoint;
}
-(BOOL)outOfChina:(double)lat bdLon:(double)lon
{
if (lon <RANGE_LON_MIN || lon > RANGE_LON_MAX)
returntrue;
if (lat <RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
returntrue;
returnfalse;
}
-(CLLocationCoordinate2D)bd09Encrypt:(double)ggLat bdLon:(double)ggLon
{
CLLocationCoordinate2D bdPt;
double x = ggLon, y = ggLat;
double z =sqrt(x * x + y * y) + 0.00002 *sin(y * M_PI);
double theta =atan2(y, x) + 0.000003 *cos(x * M_PI);
bdPt.longitude = z *cos(theta) + 0.0065;
bdPt.latitude = z *sin(theta) + 0.006;
return bdPt;
}
-(double)transformLat:(double)x bdLon:(double)y
{
double ret =LAT_OFFSET_0(x, y);
ret += LAT_OFFSET_1;
ret += LAT_OFFSET_2;
ret += LAT_OFFSET_3;
return ret;
}
-(double)transformLon:(double)x bdLon:(double)y
{
double ret =LON_OFFSET_0(x, y);
ret += LON_OFFSET_1;
ret += LON_OFFSET_2;
ret += LON_OFFSET_3;
return ret;
}
3.通过百度的网络API进行转换
http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=116.308999&y=40.059225
通过这个API传入GPS经纬度坐标,然后获取返回值进行解析,这个方法可以在多数平台使用,缺点是需要访问网络。