iOS 判斷並使用 百度地圖 高德地圖 自帶地圖 導航(使用URI,不集成sdk)

[objc] view plaincopy在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];

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章