高德地图定位偏移以及经纬度之间的转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Forever_wj/article/details/53906478

高德地图、百度地图以及CLLocationManager等地图的定位功能,从而得到的经纬度坐标会有些偏差,比如系统的CLLocationManager定位得到的是世界标准地理坐标(WGS-84)、高德SDK定位得到的是火星坐标(GCJ-02)、百度SDK定位得到的是百度地理坐标(BD-09),想要实现经纬度坐标的统一整合,就必须实现经纬度之间的转换。

  • 定义需要用到的经纬度转换算法的一些宏定义:
#define LAT_OFFSET_0(x,y) -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x))
#define LAT_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_2 (20.0 * sin(y * M_PI) + 40.0 * sin(y / 3.0 * M_PI)) * 2.0 / 3.0
#define LAT_OFFSET_3 (160.0 * sin(y / 12.0 * M_PI) + 320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0

#define LON_OFFSET_0(x,y) 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x))
#define LON_OFFSET_1 (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_2 (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0
#define LON_OFFSET_3 (150.0 * sin(x / 12.0 * M_PI) + 300.0 * sin(x / 30.0 * M_PI)) * 2.0 / 3.0

#define RANGE_LON_MAX 137.8347
#define RANGE_LON_MIN 72.004
#define RANGE_LAT_MAX 55.8271
#define RANGE_LAT_MIN 0.8293

#define jzA 6378245.0
#define jzEE 0.00669342162296594323
  • 判断是否在中国
/**
 -  @brief  判断是否在中国
 -  -  @param  location    世界标准地理坐标(WGS-84)
 -  -  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
 - (BOOL)isLocationOutOfChina:(CLLocationCoordinate2D)location {
    if (location.longitude < 72.004 || location.longitude > 137.8347 || location.latitude < 0.8293 || location.latitude > 55.8271) {
        return YES;
    } else {
        return NO;
    }
}
  • 世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
/**
 -  @brief  世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 -  -  ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
 -  -  @param  location    世界标准地理坐标(WGS-84)
 -  -  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
 - (CLLocationCoordinate2D)WGS84ToGCJ02:(CLLocationCoordinate2D)location {
    return [self GCJ02Encrypt:location.latitude BDLon:location.longitude];
}

 - (CLLocationCoordinate2D)GCJ02Encrypt:(double)ggLat BDLon:(double)ggLon
{
    CLLocationCoordinate2D resPoint;
    double mgLat;
    double mgLon;
    if ([self outOfChina:ggLat BDLon:ggLon]) {
        resPoint.latitude = ggLat;
        resPoint.longitude = ggLon;
        return resPoint;
    }
    double dLat = [self transformLat:(ggLon - 105.0)BDLon:(ggLat - 35.0)];
    double dLon = [self transformLon:(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)
        return YES;
    if (lat < RANGE_LAT_MIN || lat > RANGE_LAT_MAX)
        return YES;
    return NO;
}

 - (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;
}
  • 中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
/**
 *  @brief  中国国测局地理坐标(GCJ-02) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    中国国测局地理坐标(GCJ-02)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)GCJ02ToWGS84:(CLLocationCoordinate2D)location {
    return  [self BD09Encrypt:location.latitude BDLon:location.longitude];
}

+(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;
}
  • 世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
/**
 *  @brief  世界标准地理坐标(WGS-84) 转换成 百度地理坐标(BD-09)
 *
 *  @param  location    世界标准地理坐标(WGS-84)
 *
 *  @return 百度地理坐标(BD-09)
 */
+ (CLLocationCoordinate2D)WGS84ToBD09:(CLLocationCoordinate2D)location {
    CLLocationCoordinate2D GCJ02Pt = [self GCJ02Encrypt:location.latitude
                                                  BDLon:location.longitude];
    return [self BD09Encrypt:GCJ02Pt.latitude BDLon:GCJ02Pt.longitude];
}
  • 百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
/**
 *  @brief  百度地理坐标(BD-09) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
 *  *  @param  location    百度地理坐标(BD-09)
 *  *  @return 中国国测局地理坐标(GCJ-02)<火星坐标>
 */
 * (CLLocationCoordinate2D)BD09ToGCJ02:(CLLocationCoordinate2D)location {
    return [self BD09Decrypt:location.latitude BDLon:location.longitude];
}

 * (CLLocationCoordinate2D)BD09Decrypt:(double)BDLat BDLon:(double)BDLon {
    CLLocationCoordinate2D GCJPt;
    double x = BDLon - 0.0065, y = BDLat - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * M_PI);
    double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);
    GCJPt.longitude = z * cos(theta);
    GCJPt.latitude = z * sin(theta);
    return GCJPt;
}
  • 百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
/**
 *  @brief  百度地理坐标(BD-09) 转换成 世界标准地理坐标(WGS-84)
 *
 *  ####此接口有1-2米左右的误差,需要精确定位情景慎用
 *
 *  @param  location    百度地理坐标(BD-09)
 *
 *  @return 世界标准地理坐标(WGS-84)
 */
+ (CLLocationCoordinate2D)BD09ToWGS84:(CLLocationCoordinate2D)location {
    CLLocationCoordinate2D GCJ02 = [self BD09ToGCJ02:location];
    return [self GCJ02Decrypt:GCJ02.latitude gjLon:GCJ02.longitude];
}

+ (CLLocationCoordinate2D)GCJ02Decrypt:(double)gjLat gjLon:(double)gjLon {
    CLLocationCoordinate2D  gPt = [self GCJ02Encrypt:gjLat BDLon:gjLon];
    double dLon = gPt.longitude - gjLon;
    double dLat = gPt.latitude - gjLat;
    CLLocationCoordinate2D pt;
    pt.latitude = gjLat - dLat;
    pt.longitude = gjLon - dLon;
    return pt;
}

猜你喜欢

转载自blog.csdn.net/Forever_wj/article/details/53906478