APP應用內跳轉到其他的地圖APP導航

-(void)clickDH:(UIButton *)btn{

    NearbyGasModel *model=self.dataArray[btn.tag];

    CLLocationDegrees longitude = [NSString stringWithFormat:@"%@",model.lon].floatValue;

    CLLocationDegrees latitude = [NSString stringWithFormat:@"%@",model.lat].floatValue;

    CLLocationCoordinate2D endLocation = CLLocationCoordinate2DMake(latitude, longitude);

  

    NSArray *locationArr = [model.position componentsSeparatedByString:@","];

    CLLocationDegrees googlelongitude = [NSString stringWithFormat:@"%@",locationArr[1]].floatValue;

    CLLocationDegrees googlelatitude = [NSString stringWithFormat:@"%@",locationArr[0]].floatValue;

    CLLocationCoordinate2D googleLocation = CLLocationCoordinate2DMake(googlelongitude,googlelatitude);

    NSLog(@"lon:%f,lat:%f",longitude,latitude);

//endLocation要跳轉的經緯度-百度的    googleLocation要跳轉的經緯度-谷歌的,高德,騰訊,蘋果

    [self navThirdMapWithLocation:endLocation GoogleLocation:googleLocation andTitle:[NSString stringWithFormat:@"%@",model.name]];

}

///服務器給的數據中,經緯度是百度的lon,lat,其他的要用谷歌的經緯度position,

-(void)navThirdMapWithLocation:(CLLocationCoordinate2D)endLocation GoogleLocation:(CLLocationCoordinate2D)googleLocation andTitle:(NSString *)titleStr{

    NSMutableArray *mapsA = [NSMutableArray array];

    //蘋果原生地圖方法和其他不一樣

    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];

    iosMapDic[@"title"] = @"蘋果地圖";

    [mapsA addObject:iosMapDic];

    //高德地圖

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {

        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];

        gaodeMapDic[@"title"] = @"高德地圖";

        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=%f&dlon=%f&dname=%@&style=2",googleLocation.latitude,googleLocation.longitude,titleStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

        gaodeMapDic[@"url"] = urlString;

        [mapsA addObject:gaodeMapDic];

    }

    //百度地圖

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {

        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];

        baiduMapDic[@"title"] = @"百度地圖";

//百度自己的SDK獲取的經緯度要用bd09ll,,,其他的一般用gcj02

        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&coord_type=bd09ll&mode=driving&src=ios.blackfish.XHY",endLocation.latitude,endLocation.longitude,titleStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

        baiduMapDic[@"url"] = urlString;

        [mapsA addObject:baiduMapDic];        

        //騰訊地圖

        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {

            NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];

            qqMapDic[@"title"] = @"騰訊地圖";

            NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&to=%@&tocoord=%f,%f&coord_type=1&referer={ios.blackfish.XHY}",titleStr,googleLocation.latitude,googleLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

            qqMapDic[@"url"] = urlString;

            [mapsA addObject:qqMapDic];

        }

    }

    //手機地圖個數判斷

    if (mapsA.count > 0) {

        //選擇

        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"使用導航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        NSInteger index = mapsA.count;

        for (int i = 0; i < index; i++) {

            NSString *title = mapsA[i][@"title"];

            NSString *urlString = mapsA[i][@"url"];

            if (i == 0) {

                UIAlertAction *iosAntion = [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

                    [self appleNaiWithCoordinate:googleLocation andWithMapTitle:titleStr];

                }];

                [alertVC addAction:iosAntion];

                continue;

            }

            UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

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

            }];

            [alertVC addAction:action];

        }

        

        UIAlertAction *cancleAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];

        [alertVC addAction:cancleAct];

        [self presentViewController:alertVC animated:YES completion:^{

        }];

    }else{

    }

}

//喚醒蘋果自帶導航

- (void)appleNaiWithCoordinate:(CLLocationCoordinate2D)coordinate andWithMapTitle:(NSString *)map_title{

    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

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

    tolocation.name = map_title;

    [MKMapItem openMapsWithItems:@[currentLocation,tolocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,

                                                                               MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];

}

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