iOS --- APP 跳转百度、高德、苹果地图 定位偏差

转自 :https://www.jianshu.com/p/8ca1f116ed80

前言:我司开发了某个模块,需要地图上展示某个位置。已完成判断手机有哪类地图软件,用户可选择自定义导航地图。位置的经纬度是后台上传,前端进行跳转。

1.展示样式

image

代码展示

采用的是 URI 打来第三方地图应用。

- (void)actionSheet
{
    __block NSString *urlScheme = self.urlScheme;
    __block NSString *appName = self.appName;
    __block CLLocationCoordinate2D coordinate = self.coordinate;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //这个判断其实是不需要的
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];

            [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                           launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        }];
        [alert addAction:action];
    }

    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:action];
    }

    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//            iosamap://navi?sourceApplication=applicationName&poiname=fangheng&poiid=BGVIS&lat=36.547901&lon=104.258354&dev=1&style=2
            NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=1&style=2",appName,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:action];
    }

    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:action];
    }

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
    }];
}

2.问题展示

因为采用的是 APP 调起第三方应用,传入的都是相同的经纬度,但是偏差大约在2条街以上。
根据经纬度各第三方展示的效果如下:

image

真实位置应该是在:

image

3.问题分析

各地图经纬度查询/对比
http://www.gpsspg.com/maps.htm

经测试发现原生地图、百度地图、高德地图位置都有偏差,应该是经纬度出了问题。

image

经各方查找,原因是后台管理端录入的都是百度坐标系的经纬度,而高德和苹果,谷歌地图都是用其他坐标系的经纬度,不同坐标系会有相关偏差。

关于坐标系
我们通常用经纬度来表示一个地理位置,但是由于一些原因,我们从不同渠道得到的经纬度信息可能并不是在同一个坐标系下。不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。

  • 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
  • 百度地图使用的是BD-09坐标系
  • 底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系

WGS-84 - 世界大地测量系统
WGS-84(World Geodetic System, WGS)是使用最广泛的坐标系,也是世界通用的坐标系,GPS设备得到的经纬度就是在WGS84坐标系下的经纬度。通常通过底层接口得到的定位信息都是WGS84坐标系。

GCJ-02 - 国测局坐标
GCJ-02(G-Guojia国家,C-Cehui测绘,J-Ju局),又被称为火星坐标系,是一种基于WGS-84制定的大地测量系统,由中国国测局制定。此坐标系所采用的混淆算法会在经纬度中加入随机的偏移。国家规定,中国大陆所有公开地理数据都需要至少用GCJ-02进行加密,也就是说我们从国内公司的产品中得到的数据,一定是经过了加密的。绝大部分国内互联网地图提供商都是使用GCJ-02坐标系,包括高德地图,谷歌地图中国区等。

BD-09 - 百度坐标系
BD-09(Baidu, BD)是百度地图使用的地理坐标系,其在GCJ-02上多增加了一次变换,用来保护用户隐私。从百度产品中得到的坐标都是BD-09坐标系。

相互转换
GCJ-02和BD-09都是用来对地理数据进行加密的,所以也不会公开逆向转换的方法。理论上,GCJ-02的加密过程是不可逆的,但是可以通过一些方法来逼近接原始坐标,并且这种方式的精度很高。gcoord使用的纠偏方式达到了厘米级的精度,能满足绝大多数情况。

相关专业知识参考链接:
https://github.com/hujiulong/gcoord
https://blog.csdn.net/jssongwei/article/details/52185698
http://nightfarmer.github.io/2016/12/01/GPSUtil/

4.解决办法

坐标系的问题初步明朗,现在只需要转换相关坐标系即可。

4.1 百度地图解决办法

百度开放平台 API:http://lbsyun.baidu.com/index.php?title=uri/api/ios
coord_type该参数表示坐标,之前写的是gcj02,改成wgs84即可。

image

代码修改

NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=wgs84",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

4.2 原生地图和高德地图解决办法

原生地图和高德地图属于一类,必须转换坐标系才能减少位置偏差。
高德地图 WEB 调用 API:
http://lbs.amap.com/api/amap-mobile/guide/ios/navi
高德地图坐标系转换相关链接:
http://lbs.amap.com/api/webservice/guide/api/convert/
http://lbs.amap.com/api/ios-sdk/guide/computing-equipment/amap-calculate-tool

image

因为项目中使用了高德地图 SDK,就使用高德地图坐标系转换工具。高德地图SDK中坐标转化方法如下:

///坐标类型枚举
typedef NS_ENUM(NSUInteger, AMapCoordinateType)
{
    AMapCoordinateTypeBaidu = 0,    ///<Baidu
    AMapCoordinateTypeMapBar,       ///<MapBar
    AMapCoordinateTypeMapABC,       ///<MapABC
    AMapCoordinateTypeSoSoMap,      ///<SoSoMap
    AMapCoordinateTypeAliYun,       ///<AliYun
    AMapCoordinateTypeGoogle,       ///<Google
    AMapCoordinateTypeGPS,          ///<GPS
};

/**
 * @brief 转换目标经纬度为高德坐标系
 * @param coordinate 待转换的经纬度
 * @param type       坐标系类型
 * @return 高德坐标系经纬度
 */
FOUNDATION_EXTERN CLLocationCoordinate2D AMapCoordinateConvert(CLLocationCoordinate2D coordinate, AMapCoordinateType type);

代码修改

//1.通过高德地图坐标转换
//2.在高德、原生地图传入转换过的经纬度
CLLocationCoordinate2D amapcoord = AMapCoordinateConvert(CLLocationCoordinate2DMake(39.989612,116.480972), AMapCoordinateTypeBaidu);
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&poiname=fangheng&poiid=BGVIS&lat=%f&lon=%f&dev=1&style=2",appName,amapcoord.latitude, amapcoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

相关链接
各地图经纬度拾取:http://www.gpsspg.com/maps.htm
百度地图经纬度拾取:http://api.map.baidu.com/lbsapi/getpoint/index.html
高德地图经纬度拾取:http://lbs.amap.com/console/show/picker

总结:由于各地图使用的坐标系不同,直接代入同一坐标系就会有地理偏差,本文提供相应解决办法,但深入了解坐标系就靠地理专业人员了。在排雷爬坑过程中总是增加见识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值