博客> 坐标转换百度,高德,WGS84地心坐标系
坐标转换百度,高德,WGS84地心坐标系
2020-01-26 17:28 评论:0 阅读:682 superjunjun
高德地图 百度地图 WGS84坐标系转换

在以前开发一款定位手表的时候,手表返回的是真实的地心坐标系,项目中需要转换为高德,上代码

  • (CLLocationCoordinate2D)changeWGS84ToGcj02WithLatitude:(double )latitude withLongitude:(double )longitude { // if([self outOfChina:latitude withLongitude:longitude]) // { // return NULL; // } CLLocationCoordinate2D coord; double dLatitude = [self transformLatWithX:longitude - 105.0 WithY:(latitude - 35.0)]; double dLongitude = [self transformLonWithX:longitude - 105.0 WithY:latitude - 35.0]; double radLat = latitude / 180.0 M_PI; double magic = sin(radLat); magic = 1 - ee magic magic; double sqrtMagic = sqrt(magic); dLatitude = (dLatitude 180.0) / ((a (1 - ee)) / (magic sqrtMagic) M_PI); dLongitude = (dLongitude 180.0) / (a / sqrtMagic cos(radLat) M_PI); double mgLat = latitude + dLatitude; double mgLon = longitude + dLongitude; coord = CLLocationCoordinate2DMake(mgLat, mgLon); return coord; } //是否在中国国内
  • (BOOL)outOfChina:(double )latitude withLongitude:(double )longitude { if((longitude < 72> 137.8347) || (latitude < 0> 55.8271)) { return YES; } else { return NO; } }

  • (double)transformLatWithX:(double )x WithY:(double )y { double ret = -100.0 + 2.0 x + 3.0 y + 0.2 y y + 0.1 x y

    • 0.2 sqrt(fabs(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 sin(y M_PI / 30.0)) 2.0 / 3.0; return ret; }
  • (double)transformLonWithX:(double )x WithY:(double )y { double ret = 300.0 + x + 2.0 y + 0.1 x x + 0.1 x * y + 0.1

    • sqrt(fabs(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; }
  • (NSString )changeGcj02ToWGS84WithLatitude:(double )latitude withLongitude:(double )longitude { // CLLocationCoordinate2D coord; double dLat = [self transformLatWithX:longitude - 105.0 WithY:latitude - 35.0]; double dLon = [self transformLonWithX:longitude - 105.0 WithY:latitude - 35.0]; double radLat = 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); double mgLat = latitude + dLat; double mgLon = longitude + dLon;

    return [NSString stringWithFormat:@"%f %f ",longitude 2 - mgLon ,latitude 2 - mgLat]; }

最终因为项目需要国际化,最终使用了苹果自带地图,苹果在大陆实用的是高德地图的数据,所以不影响。

百度地图坐标系 与 高德地图坐标系的相互转化 上代码

  • (CLLocationCoordinate2D)changeBd09ToGcj02WithLatitude:(double )latitude withLongitude:(double )longitude{

    double x = longitude - 0.0065, y = 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); }

  • (CLLocationCoordinate2D)changeGcj02ToBd09WithLatitude:(double )latitude withLongitude:(double )longitude{

    double x = longitude, y = 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); }

收藏
0
sina weixin mail 回到顶部