临时收藏iOS开发实用技术iOS开发资料收集区

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

2017-03-17  本文已影响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];
    }
}
上一篇 下一篇

猜你喜欢

热点阅读