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

[objc]  view plain copy
  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];

}




你可能感兴趣的:(iOS)