iOS定位坐标转换工具

如果你正需要这样的工具,保证拿过去就能用
如果你还不知道这是什么工具,我保证以后的iOS开发中肯定会用
如果你是iOS大牛,期待您能指点一下 ^_^

好了,废话不多说了。上干货


坐标系介绍

首先介绍一下目前的定位坐标系统
1、地球坐标 :( 代号:GPS、WGS84 )--- 有W就是世界通用的
也就是原始坐标体系,这是国际公认的世界标准坐标体系;

使用 WGS84 坐标系统的产品有
苹果的 CLLocationManager 获取的坐标


2、火星坐标: (代号:GCJ-02)--- G国家 C测绘 J局 02年测绘的
为了保证国家安全,不被逮人获知精准的地点,国内使用的一切坐标,都必须是经过测绘局精密处理的坐标。你要是不用?估计你也不能不用,呵呵

使用 GCJ-02 火星坐标系统的产品有
高德地图腾讯地图阿里云地图灵图51地图

注意:现在苹果系统自带的地图使用的是高德地图,所以苹果地带的地图应用,用的是GCJ-02的坐标系统。但是代码中CLLocationManager获取到的是WGS84坐标系的坐标


3、其他坐标 :百度坐标系统 (代号:BD-09)
大百度地图的坐标系统,岂能跟你们一样。百度坐标是在火星坐标的基础上再次加密计算而获得的

使用 BD-09 坐标系统的产品有
百度地图

坐标转换

开发中你会遇到这样的需求
1、通过当前和地址的经纬度,点击导航按钮
2、如果手机装有百度地图,就跳转百度地图导航
3、如果没有百度,有高德地图,就跳转到高德地图导航
4、如果在没有,就跳转腾讯地图导航
如果········来人,把产品经理拖出去,宰掉!!!

在开发过程中,通过 CLLocationManager 拿到地球坐标坐标。就可以在跳转不同导航产品之前,进行坐标转换

地球坐标 ---> 火星坐标

- (NSDictionary *)locationMarsFromEarth_earthLat:(double)latitude earthLon:(double)longitude {
    // 首先判断坐标是否属于天朝
    if (![self isInChinaWithlat:latitude lon:longitude]) {
        return @{@"latitude":@(latitude),
                 @"longitude":@(longitude)
                };
    }
    double a = 6378245.0;
    double ee = 0.00669342162296594323;

    double dLat = [self transform_earth_from_mars_lat_lat:(latitude - 35.0) lon:(longitude - 35.0)];
    double dLon = [self transform_earth_from_mars_lng_lat:(latitude - 35.0) lon:(longitude - 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 newLatitude = latitude + dLat;
    double newLongitude = longitude + dLon;
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}
- (BOOL)isInChinaWithlat:(double)lat lon:(double)lon {
    if (lon < 72.004 || lon > 137.8347)
        return NO;
    if (lat < 0.8293 || lat > 55.8271)
        return NO;
    return YES;
}
- (double)transform_earth_from_mars_lat_lat:(double)y lon:(double)x {
    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) + 3320 * sin(y * M_PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

- (double)transform_earth_from_mars_lng_lat:(double)y lon:(double)x {
    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;
}

火星坐标 <---> 百度坐标

/** 百度坐标 => 火星坐标 */
- (NSDictionary *)marsLocationFromBaidu_baiduLat:(double)latitude baiduLon:(double)longitude {
    double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    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 newLatitude = z * sin(theta);
    double newLongitude = z * cos(theta);
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}


/** 火星坐标 => 百度坐标 */
- (NSDictionary *)baiduLocationFromMars_marsLat:(double)latitude marsLon:(double)longitude {
    double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    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 newLatitude = z * sin(theta) + 0.006;
    double newLongitude = z * cos(theta) + 0.0065;
    NSDictionary *dic = @{@"latitude":@(newLatitude),
                          @"longitude":@(newLongitude)
                          };
    return dic;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值