iOS 判断并使用 百度地图 高德地图 自带地图 导航(使用URI,不集成sdk)

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. BOOL hasBaiduMap = NO;  
  2.         BOOL hasGaodeMap = NO;  
  3.           
  4.         if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]){  
  5.             hasBaiduMap = YES;  
  6.         }  
  7.         if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"iosamap://"]]){  
  8.             hasGaodeMap = YES;  
  9.         }  
  10.       
  11.   
  12.     if ([@"使用百度地图导航" isEqualToString:title])  
  13.         {  
  14.             NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",currentLat, currentLon,_shopLat,_shopLon] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;  
  15.               
  16.             [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];  
  17.         }  
  18.         else if ([@"使用高德地图导航" isEqualToString:title])  
  19.         {  
  20.             NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2",@"app name", yourscheme, @"终点", _shopLat, _shopLon] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
  21.   
  22.             [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];  
  23.         }  






#pragma mark--导航按钮

- (void)navButtonClick:(UIButton *)sender {


    //检测装了哪些地图应用

    NSURL *baiDuAppUrl = [NSURL URLWithString:@"baidumap://location?id=1"];

    BOOL hasBaiDuMap = [[UIApplication sharedApplication]canOpenURL:baiDuAppUrl];

    

    NSURL *gaoDeMapUrl = [NSURL URLWithString:@"iosamap://location?id=1"];

    BOOL hasgaoDeMap = [[UIApplication sharedApplication]canOpenURL:gaoDeMapUrl];

    

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    

    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    [alertController addAction:action2];

    if (hasBaiDuMap) {

        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"使用百度地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        

            NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving",self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude, self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;

            

            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];

            

        }];

        [alertController addAction:action3];

    } if (hasgaoDeMap) {

        UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"使用高德地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            

            NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"ChengMi",@"iosChengmi",self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

         }];

        [alertController addAction:action4];

    }

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"使用苹果自带地图导航" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        

        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

        //获取到目标位置的坐标

        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.poiList.poiLat.floatValue, self.poiList.poiLng.floatValue);

        //MKMapItem的特点如下:是OC API 可以通过一个或者多个pins来打开地图,直接转至某个地方,定制地图的显示。

        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];

        //打开系统地图

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

    }];

    

    [alertController addAction:action1];


    [self presentViewController:alertController animated:YES completion:nil];

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值