iOS开发中的火星坐标系及各种坐标系转换算法
源:https://my.oschina.net/u/2607703/blog/619183
 

其原理是这样的:保密局开发了一个系统,能将实际的坐标转换成虚拟的坐标。所有在中国销售的数字地图必须使用这个系统进行坐标转换之后方可上市。这是生产环节,这种电子地图被称为火星地图。在使用环节,GPS终端设备必须集成保密局提供的加密算法(集成工作由保密局完成),把从GPS卫星那里得到的坐标转换成虚拟坐标,然后再去火星地图上查找,这样就在火星坐标系上完成了地图的匹配。 所以大家所用的百度,高德等地图定位准是偏差几百米

名词总结:

地球坐标:指WGS84坐标系统

火星坐标:指使用国家保密插件人为偏移后的坐标
地球地图:指与地球坐标对应的客观真实的地图
火星地图:指经过加密偏移后的,与火星坐标对应的地图

坐标系转换算法

1.GCJ-02(火星坐标系)和BD-09转换

// 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);
}

2.WGS-84(地球坐标系)和BD-09(百度坐标)转换

// 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;
}

3.WGS-84和sogou坐标转换

// 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);
}

4火星坐标和地球坐标转换

// World Geodetic System ==> Mars Geodetic System
+ (CLLocationCoordinate2D)WorldGS2MarsGS:(CLLocationCoordinate2D)coordinate
{
    // a = 6378245.0, 1/f = 298.3
    // b = a * (1 - f)
    // ee = (a^2 - b^2) / a^2;
    const double a = 6378245.0;
    const double ee = 0.00669342162296594323;

    if (outOfChina(coordinate.latitude, coordinate.longitude))
    {
        return coordinate;
    }
    double wgLat = coordinate.latitude;
    double wgLon = coordinate.longitude;
    double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
    double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
    double radLat = wgLat / 180.0 * PI;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * PI);

    return CLLocationCoordinate2DMake(wgLat + dLat, wgLon + dLon);
}

// Mars Geodetic System ==> World Geodetic System
+ (CLLocationCoordinate2D)MarsGS2WorldGS:(CLLocationCoordinate2D)coordinate
{
    double gLat = coordinate.latitude;
    double gLon = coordinate.longitude;
    CLLocationCoordinate2D marsCoor = [ALDGeocoder WorldGS2MarsGS:coordinate];
    double dLat = marsCoor.latitude - gLat;
    double dLon = marsCoor.longitude - gLon;
    return CLLocationCoordinate2DMake(gLat - dLat, gLon - dLon);
}

5WGS-84 和 墨卡托 坐标转换

//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);
}

开发时所面临的现状

获取经纬度(GPS)

  • 火星坐标

    • MKMapView

  • 地球坐标

    • CLLocationManager

显示经纬度(地图)

  • 火星坐标

    • iOS 地图

    • Gogole地图

    • 搜搜、阿里云、高德地图

  • 地球坐标

    • Google 卫星地图(国外地图应该都是……)

  • 百度坐标

    • 百度地图

推荐的解决方案:

  • 既然是在国内,存储一律用火星坐标,这样在使用国内地图显示时最方便(用百度地图显示时可以一次转换取得)

  • CLLocationManager 拿到的 CLLocation 转为火星坐标,MKMapView 不用处理

  • 使用地图 API 进行 地址解析/逆地址解析(Geocoding) 时注意相应使用相应地图商的坐标系

  • 部分地图商支持多个坐标系输入,如高德支持地球、火星坐标(这个一直有变动,具体只能参考厂商最新文档了

[转]iOS开发中的火星坐标系及各种坐标系转换算法的更多相关文章

  1. iOS开发中的火星坐标系及各种坐标系转换算法

    原文地址:http://m.oschina.net/blog/619183?ref=myread 其原理是这样的:保密局开发了一个系统,能将实际的坐标转换成虚拟的坐标.所有在中国销售的数字地图必须使用 ...

  2. IOS开发中与设计沟通之字体大小转换

    px:相对长度单位.像素(Pixel).pt:绝对长度单位.点(Point).1in = 2.54cm = 25.4 mm = 72pt = 6pc 具体换算是: Points Pixels Ems ...

  3. iOS开发中关于UIImage的知识点总结

    UIImage是iOS中层级比较高的一个用来加载和绘制图像的一个类,更底层的类还有 CGImage,以及iOS5.0以后新增加的CIImage.今天我们主要聊一聊UIImage的三个属性: image ...

  4. IOS开发中的CGFloat、CGPoint、CGSize和CGRect

    IOS开发中的CGFloat.CGPoint.CGSize和CGRect http://developer.apple.com/library/ios/#documentation/GraphicsI ...

  5. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  6. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  7. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

  8. ios开发中的小技巧

    在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...

  9. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

随机推荐

  1. EntityFramework之异步、事务及性能优化(九)

    前言 本文开始前我将循序渐进先了解下实现EF中的异步,并将重点主要是放在EF中的事务以及性能优化上,希望通过此文能够帮助到你. 异步 既然是异步我们就得知道我们知道在什么情况下需要使用异步编程,当等待 ...

  2. EntityFramework之孩子删除(四)(你以为你真的懂了?)

    前言 从表面去看待事物视线总有点被层层薄雾笼罩的感觉,当你静下心来思考并让指尖飞梭于键盘之上,终将会拨开浓雾见青天.这是我切身体验. 在EF关系配置中,我暂且将主体对象称作为父亲,而依赖对象称作为孩子 ...

  3. hdu4831 Scenic Popularity(线段树)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4831 题目大概意思就是有多个风景区和休息区,每个风景区有热度,休息区的热度与最接近的分景区的热度相同, ...

  4. 微服务实战(二):使用API Gateway--转

    原文地址:http://dockone.io/article/482 [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用的理想选 ...

  5. 1Z0-053 争议题目解析330

    1Z0-053 争议题目解析330 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 330.What will be the end result of this set of RM ...

  6. WebGIS开源方案中空间数据的入库、编辑、发布的操作流程

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 本开源方案的构架是:geoserver(服务器)+tomca ...

  7. jQuery-1.9.1源码分析系列(一)整体架构续

    这一节主要是jQuery中最基础的几个东东 2.    jQuery的几个基础属性和函数 a. jQuery.noConflict函数详解 在jQuery初始化的时候保存了外部的$和jQuery _j ...

  8. PR&AE插件开发遇到的一个坑

    经过一段时间的摸索,对Adobe Premiere Pro和After Effects系列插件的开发工作有了一定的掌握.如今公司需要针对Premiere Pro和After Effects开发另外一款 ...

  9. 使用签名来保证ASP.NET MVC OR WEBAPI的接口安全

    当我们开发一款App的时候,App需要跟后台服务进行通信获取或者提交数据.如果我们没有完善的安全机制则很容易被别用心的人伪造请求而篡改数据. 所以我们需要使用某种安全机制来保证请求的合法.现在最常用的 ...

  10. clientTarget与用户代理别名

    将特定用户代理的别名添加到用户代理别名的内部集合中. 来自 <https://msdn.microsoft.com/zh-cn/library/6379d90d(v=vs.110).aspx&g ...