赞
踩
目前市面上比较常见的互联网地图的坐标系主要有这样几种:GCJ-02、BD-09、WGS84、CGCS2000。 WGS84,原始坐标体系。主要有Google Earth在用,。 GCJ-02是由国测局制定的互联网地图坐标系,又叫火星坐标,最常见的互联网地图坐标系,在中国能见到的互联网 地图基本都是这种坐标了,比如高德地图、腾讯地图、百度地图、Google地图(中国范围)。 BD-09是百度地图独有的坐标系,是在GCJ-02的基础之上进行二次加密的地图坐标,比GCJ-02坐标偏了几百米 的样子。 CGCS2000是国家2000坐标系,是一个地心坐标系,目前的话应该就只有天地图在用了,所以目前的互联网地图就 只有天地图使用的是真实坐标,其他都是使用的加密坐标。 CGCS2000、WGS84都是地心坐标系,地心与参心不同,参心坐标系是以参考椭球为基准建立的坐标系,不同的国 家有着自己的参考椭球标准,所以会存在一些差异性,而地心坐标系是以地球的质量中心为基准建立的坐标系统, 所以,2000与84基本是重合的,只有高程基准面会存在差异,天地图的定位API就是直接读取的手机GPS坐标,不 加密直接显示到地图上。
//地球坐标系转火星坐标系方法
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
+ (CLLocation *)transformToMars:(CLLocation *)location {
//是否在中国大陆之外
if ([[self class] outOfChina:location]) {
return location;
}
double dLat = [[self class] transformLatWithX:location.coordinate.longitude - 105.0 y:location.coordinate.latitude - 35.0];
double dLon = [[self class] transformLonWithX:location.coordinate.longitude - 105.0 y:location.coordinate.latitude - 35.0];
double radLat = location.coordinate.latitude / 180.0 * M_PI;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * M_PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
return [[CLLocation alloc] initWithLatitude:location.coordinate.latitude + dLat longitude:location.coordinate.longitude + dLon];
}
+ (BOOL)outOfChina:(CLLocation *)location {
if (location.coordinate.longitude < 72.004 || location.coordinate.longitude > 137.8347) {
return YES;
}
if (location.coordinate.latitude < 0.8293 || location.coordinate.latitude > 55.8271) {
return YES;
}
return NO;
}
+ (double)transformLatWithX:(double)x y:(double)y {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * M_PI) + 320.0 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
return ret;
}
+ (double)transformLonWithX:(double)x y:(double)y {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0;
ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0;
return ret;
}
// GCJ-02 坐标转换成 BD-09 坐标
+ (CLLocationCoordinate2D)MarsGS2BaiduGS:(CLLocationCoordinate2D)coordinate
{
double x_pi = PI * 3000.0 / 180.0;
double x = coordinate.longitude, y = coordinate.latitude;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
double bd_lon = z * cos(theta) + 0.0065;
double bd_lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(bd_lat, bd_lon);
}
// BD-09 坐标转换成 GCJ-02 坐标
+ (CLLocationCoordinate2D)BaiduGS2MarsGS:(CLLocationCoordinate2D)coordinate
{
double x_pi = PI * 3000.0 / 180.0;
double x = coordinate.longitude - 0.0065, y = coordinate.latitude - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
double gg_lon = z * cos(theta);
double gg_lat = z * sin(theta);
return CLLocationCoordinate2DMake(gg_lat, gg_lon);
}
// WGS-84 坐标转换成 BD-09 坐标
+ (CLLocationCoordinate2D)WorldGS2BaiduGS:(CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D mars = [ALDGeocoder WorldGS2MarsGS:coordinate];
CLLocationCoordinate2D baidu = [ALDGeocoder MarsGS2BaiduGS:mars];
return baidu;
}
// BD-09 坐标转换成 WGS-84 坐标
+ (CLLocationCoordinate2D)BaiduGS2WorldGS:(CLLocationCoordinate2D)coordinate
{
CLLocationCoordinate2D mars = [ALDGeocoder BaiduGS2MarsGS:coordinate];
CLLocationCoordinate2D world = [ALDGeocoder MarsGS2WorldGS:mars];
return world;
}
// WGS-84 坐标转换成 Sogou 坐标
+ (CLLocationCoordinate2D)WorldGS2SogouGS:(CLLocationCoordinate2D)coordinate
{
const double ee = 0.082271854224939184;
double lon = coordinate.longitude;
double lat = coordinate.latitude;
double dlon = [ALDGeocoder rad:CLIP(lon, -360, 360)];
double dlat = [ALDGeocoder rad:CLIP(lat, -90, 90)];
dlon = 6378206.4 * dlon;
double sinphi = sin(dlat);
double temp1, temp2;
if((temp1 = 1.0 + sinphi) == 0.0){
dlat = -1000000000;
}else if((temp2 = 1.0 - sinphi) == 0.0){
dlat = 1000000000;
}else{
double esinphi = ee * sinphi;
dlat = 3189103.2000000002 * log((temp1 / temp2) * pow((1.0 - esinphi) / (1.0 + esinphi), ee));
}
return CLLocationCoordinate2DMake(dlat, dlon);
}
// Sogou 坐标转换成 WGS-84 坐标
+ (CLLocationCoordinate2D)SogouGS2WorldGS:(CLLocationCoordinate2D)coordinate
{
const double ee = 1.5707963267948966;
const double aa = 0.0033938814110493522;
double lon = coordinate.longitude;
double lat = coordinate.latitude;
double dlon = lon / 6378206.4;
double temp = -lat / 6378206.4;
double chi;
if(temp < -307){
chi = ee;
}else if(temp > 308){
chi = -ee;
}else{
chi = ee - 2 * atan(exp(temp));
}
double chi2 = 2 * chi;
double coschi2 = cos(chi2);
double dlat = chi + sin(chi2) * (aa + coschi2 * (1.3437644537757259E-005 + coschi2 * (7.2964865099246009E-008 + coschi2 * 4.4551470401894685E-010)));
double rlon = CLIP([ALDGeocoder deg:dlon], -360, 360);
double rlat = CLIP([ALDGeocoder deg:dlat], -90, 90);
return CLLocationCoordinate2DMake(rlat, rlon);
}
//WGS-84 坐标转换成 墨卡托 坐标
+ (CLLocationCoordinate2D)WorldGS2Mercator:(CLLocationCoordinate2D)coordinate
{
double lon = coordinate.longitude*20037508.34/180;
double lat = log(tan((90+coordinate.latitude)*M_PI/360))/(M_PI/180);
lat = lat*20037508.34/180;
return CLLocationCoordinate2DMake(lat, lon);
}
//墨卡托 坐标转换成 WGS-84 坐标
+ (CLLocationCoordinate2D)Mercator2WorldGS:(CLLocationCoordinate2D)mercator
{
double lon = mercator.longitude/20037508.34*180;
double lat = mercator.latitude/20037508.34*180;
lat = 180/M_PI*(2*atan(exp(lat*M_PI/180))-M_PI/2);
return CLLocationCoordinate2DMake(lat, lon);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。