iOS调用第三方地图App进行导航方法

常见第三方地图App的url Scheme

  • 百度地图:baidumap
  • 高德地图:iosamap
  • 谷歌地图:comgooglemaps
  • 腾讯地图:qqmap

info.plist白名单设置

plist文件新增LSApplicationQueriesSchemes关键字,类型为NSArray,并在其下添加子目录,类型为NSString,内容为各地图对应的url Scheme。

iOS调用第三方地图App进行导航方法_第1张图片

代码实例

* longitude 经度

* latitude 纬度

 * name 目的地的名字

  [self navigationLocationTitle:name latitudeText:latitude longitudeText:longitude];

-(void)navigationLocationTitle:(NSString *)title latitudeText:(NSString *)latitude longitudeText:(NSString *)longitude {

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

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

    NSMutableArray *maps = [NSMutableArray array];

    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];

    iosMapDic[@"title"] = @"苹果地图";

    [maps addObject:iosMapDic];

  //百度地图

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

               NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];

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

               NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name=%@&mode=driving&coord_type=gcj02",latitude,longitude,title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

               baiduMapDic[@"url"] = urlString;

               [maps addObject:baiduMapDic];

           }



           //高德地图

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

               NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];

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

               NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&sid=BGVIS1&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0",@"辰邦急救",@"iosorun",latt,lon,title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

               gaodeMapDic[@"url"] = urlString;

               [maps addObject:gaodeMapDic];

           }



           //谷歌地图

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

               NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];

               googleMapDic[@"title"] = @"谷歌地图";

               NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%@&directionsmode=driving",@"导航测试",@"nav123456",title] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

               googleMapDic[@"url"] = urlString;

               [maps addObject:googleMapDic];

           }

           //腾讯地图

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

               NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];

               qqMapDic[@"title"] = @"腾讯地图";

               NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%@,%@&to=%@&coord_type=1&policy=0",latitude,longitude,title ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

               qqMapDic[@"url"] = urlString;

               [maps addObject:qqMapDic];

           }

           UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:@"导航去急救站" preferredStyle:UIAlertControllerStyleActionSheet];

           NSInteger index = maps.count;

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

               NSString * actititle = maps[i][@"title"];

               //苹果原生地图方法

               if (i == 0) {



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

                      self navAppleMapTitle:title latitudeText:latitude longitudeText:longitude];

                   }];

                   [alert addAction:action];



               }else{

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

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

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

                  }];

                  [alert addAction:action];

               }

           }

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

               }];

        [alert addAction:cancleAct];

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

        }];

}

//苹果地图
- (void)navAppleMapTitle:(NSString *)mapTitle latitudeText:(NSString *)latitude longitudeText:(NSString *)longitude
{

    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
    toLocation.name = mapTitle;
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    [MKMapItem openMapsWithItems:items launchOptions:dic];

}

 

你可能感兴趣的:(iOS)