美文网首页临时收藏iOS开发实用技术iOS开发资料收集区
iOS开发笔记 | 跳转到百度、高德or苹果地图进行导航

iOS开发笔记 | 跳转到百度、高德or苹果地图进行导航

作者: 无夜之星辰 | 来源:发表于2017-03-17 21:57 被阅读3395次
iu

首先要记住:苹果地图和高德地图的坐标系一样,百度地图和高德地图的坐标系不一样。

做这些功能首先肯定是查官方文档,怎么查?以高德地图为例:


百度地图文档在这:
http://lbsyun.baidu.com/index.php?title=uri/api/ios


具体步骤:

1. 设置白名单


参考代码

#pragma mark - 去地图展示路线
/** 去地图展示路线 */
- (void)gotoMap{
    // 后台返回的目的地坐标是百度地图的
    // 百度地图与高德地图、苹果地图采用的坐标系不一样,故高德和苹果只能用地名不能用后台返回的坐标
    CGFloat latitude  = _consigneeModel.latitude.floatValue;  // 纬度
    CGFloat longitude = _consigneeModel.longitude.floatValue; // 经度
    NSString *address = _consigneeModel.address; // 送达地址
    
    // 打开地图的优先级顺序:百度地图->高德地图->苹果地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        // 百度地图
        // 起点为“我的位置”,终点为后台返回的坐标
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%f,%f&mode=riding&src=快健康快递", latitude, longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
    }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        // 高德地图
        // 起点为“我的位置”,终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com"]]){
        // 苹果地图
        // 起点为“我的位置”,终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }else{
        // 快递员没有安装上面三种地图APP,弹窗提示安装地图APP
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请安装地图APP" message:@"建议安装百度地图APP" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertVC animated:NO completion:nil];
    }
}

相关文章

网友评论

  • Mars飘殇:请问能否设置途径点的导航?
  • appear_me:我想请教一下,拿百度地图来说,它后台返回的是终点位置是“地图上的点”,怎样才能让它显示我放进去的目的地的名字呢?
    无夜之星辰:@appear_me 额,看看文档吧,你问我我也是看文档:sweat_smile:
  • 钱塘老酒酿:高德地图,BGVIS1 这个是你注册后生成的东西么。高德地图这样调用,是否需要 导入sdk包啊
    钱塘老酒酿:@无夜之星辰 好的 这个实用性就👍了
    无夜之星辰:@儡小傀 不需要注册也不需要导入sdk
  • 谢谢生活:刚集成过

本文标题:iOS开发笔记 | 跳转到百度、高德or苹果地图进行导航

本文链接:https://www.haomeiwen.com/subject/yxitnttx.html