您的位置:首页 > 其它

火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法

2015-10-13 16:54 387 查看
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

//火星坐标系 (GCJ-02) 转换 百度坐标系 (BD-09)
+ (CLLocationCoordinate2D)convertToBaiduCoordinate:(CLLocationCoordinate2D)gcjCoordinate {
double x = gcjCoordinate.longitude;
double y = gcjCoordinate.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;

NSLog(@"%f, %f", bd_lon, bd_lat);
return CLLocationCoordinate2DMake(bd_lat, bd_lon);
}

//百度坐标系 (BD-09) 转换 火星坐标系 (GCJ-02)
+ (CLLocationCoordinate2D)convertToGCJCoordinate:(CLLocationCoordinate2D)baiduCoordinate {
double x = baiduCoordinate.longitude - 0.0065;
double y = baiduCoordinate.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);

NSLog(@"%f, %f", gg_lon, gg_lat);
return CLLocationCoordinate2DMake(gg_lat, gg_lon);
}


不过也有更简单的算法,线性算法(lat和lng是经纬度,球面坐标):

To_B是转到百度,To_G是转到GCJ-02。

var TO_BLNG = function(lng){return lng+0.0065;};

var TO_BLAT = function(lat){return lat+0.0060;};

var TO_GLNG = function(lng){return lng-0.0065;};

var TO_GLAT = function(lat){return lat-0.0060;};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: