iOS调用高德地图导航

    在iOS开发地图模块中,有需要用到导航的功能,尤其类似一些送快递、外卖等软件,除了需要展示路线到地图中,还需要有一个导航按钮。一般导航功能分两类:一类是在本APP内部调用高德API的导航页面,即在APP内部集成导航模块,此类导航页面可以自己定制界面,但基本功能都是调用高德封装好的方法即可。另一类就是点击导航按钮,跳转到手机内装的地图类APP里去进行导航。

    今天我要说的是第二类,点击跳转到地图APP里去导航。因为第一类导航,在对应的API文档和Demo里已经有详细的文档来说明如何集成。

    下面是点击导航按钮需要的代码:

  

//MARK:导航
- (void)navBtnClicked:(UIButton *)sender
{
    
    // 后台返回的目的地坐标是百度地图的
    // 百度地图与高德地图、苹果地图采用的坐标系不一样,故高德和苹果只能用地名不能用后台返回的坐标
    CGFloat latitude  = (CGFloat)endLat;  // 纬度
    CGFloat longitude = (CGFloat)endLng; // 经度
//    __block NSString *address = @"";// 送达地址
    self.address = [[NSString alloc] init];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude];
    CLGeocoder * geocoder = [[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error == nil && [placemarks count] > 0) {
            
            //这时的placemarks数组里面只有一个元素
            
            CLPlacemark * placemark = [placemarks firstObject];
            NSLog(@"%@",placemark.addressDictionary); //根据经纬度会输出该经纬度下的详细地址  国家 地区 街道 之类的
            self.address = [NSString stringWithFormat:@"%@",placemark.addressDictionary[@"FormattedAddressLines"][0]] ;
            [self openAlert];
            
        }
    }];
    
    

}
- (void)openAlert
{
    CGFloat latitude  = (CGFloat)endLat;  // 纬度
    CGFloat longitude = (CGFloat)endLng; // 经度
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:(CLLocationDegrees)longitude];
    
    // 打开地图的优先级顺序:百度地图->高德地图->苹果地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        // 高德地图
        // 起点为“我的位置”,终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&sname=%@&did=BGVIS2&dname=%@&dev=0&t=0",@"我的位置",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) {
            
        }];
        
        
    }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        // 百度地图
        // 起点为“我的位置”,终点为后台返回的坐标
        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 options:nil completionHandler:^(BOOL success) {
            
        }];
    }else if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com"]]){
        // 苹果地图
        // 起点为“我的位置”,终点为后台返回的address
        NSString *urlString = [[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",self.address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:^(BOOL success) {
            
        }];
    }else{
        // 快递员没有安装上面三种地图APP,弹窗提示安装地图APP
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请安装地图APP" message:@"建议安装高德地图APP" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertVC animated:NO completion:nil];
    }
}

    以上就是设置导航的一种方法,此外,不愿意敲代码的同学可以直接在info.plist里直接配置几种地图APP的跳转url,这种方法可以达到以上同样的效果。

    另外,这种调用方式的一个问题我还未解决,就是在直接跳转高德APP时,终点位置首先会弹出一个位置列表让你点选导航终点,有时项目可能不需要这种周边搜索选择终点,但是我还未找到方法如何避免这个搜索列表的展示,知道解决办法的大神希望帮忙解答一下。

猜你喜欢

转载自blog.csdn.net/zhonglv_honeymoon/article/details/79741445