iOS APP内跳转到各类地图进行路线规划

 1,设置开启开启定位权限

Privacy - Location When In Use Usage Description

2,plist中添加白名单(LSApplicationQueriesSchemes)才能检查某个地图是否已经安装:

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

#pragma mark "选择地图类型进行导航"
static NSArray * _mapArr;

- (void)chooseMapWithLocationCoordinate2D:(CLLocationCoordinate2D)coordinate appName:(NSString *)appName backScheme:(NSString *)backScheme adress:(NSString *)address
{
    // 查看本机所安装的地图app
    _mapArr = [self getInstalledMapAppWithEndLocation:coordinate appName:appName backScheme:backScheme adress:address];
    // 创建UIActionSheet
    UIActionSheet * _mapActionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择地图进行导航" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:nil];
    for (NSDictionary *dict in _mapArr)
    {
        [_mapActionSheet addButtonWithTitle:dict[@"title"]];
    }
    [_mapActionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
   if (buttonIndex)
    {
        buttonIndex==1?[self navAppleMapWithDict:_mapArr[buttonIndex-1]]:
        [_mapArr[buttonIndex-1][@"url"] openUrl];
        
    }
    
}


#pragma mark - 导航方法
- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation appName:(NSString *)appName backScheme:(NSString *)backScheme adress:(NSString *)address
{
    NSMutableArray *maps = [NSMutableArray array];
    CLLocationCoordinate2D amapcoord = [self bdToGaodeWithLat:endLocation.latitude andLon:endLocation.longitude];
    //苹果地图
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
    iosMapDic[@"title"] = @"苹果地图";
    iosMapDic[@"lat"] = [NSNumber numberWithDouble:endLocation.latitude];
    iosMapDic[@"lon"] = [NSNumber numberWithDouble:endLocation.longitude];
    [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=name:商家位置|latlng:%f,%f&mode=driving",endLocation.latitude,endLocation.longitude] 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=%@&sname=我的位置&dlat=%lf&dlon=%lf&dname=%@&dev=0&style=2&m=0&t=0",appName,backScheme,amapcoord.latitude,amapcoord.longitude,@"商家位置"] 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=%f,%f&directionsmode=driving",appName,backScheme,amapcoord.latitude, amapcoord.longitude] 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=%f,%f&to=商家位置&coord_type=1&policy=0",amapcoord.latitude, amapcoord.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        qqMapDic[@"url"] = urlString;
        [maps addObject:qqMapDic];

    }
    
    return maps;
}

// 苹果地图
- (void)navAppleMapWithDict:(NSDictionary *)dict
{
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake([dict[@"lat"] doubleValue], [dict[@"lng"] doubleValue]) addressDictionary:nil]];
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)
                          };
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}


// 百度转其他地图经纬度

-(CLLocationCoordinate2D)bdToGaodeWithLat:(double)lat andLon:(double)lon

{

//   double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    double x = (lon-0.0065);

    double y = (lat-0.006);

    double z = sqrt(x*x + y*y) - 0.00002*sin(y*M_PI);

    double theta = atan2(y, x) - 0.000003 * cos(x * M_PI);

    NSLog(@"bd_lon:%f",z * cos(theta) );

    NSLog(@"bd_lat:%f",z * sin(theta) );
    return CLLocationCoordinate2DMake(z * sin(theta) , z * cos(theta));


}

使用的时候直接调用代码方法:

 

  [self chooseMapWithLocationCoordinate2D:CLLocationCoordinate2DMake([lat floatValue], [lon floatValue]) appName:isApp_Name backScheme:backScheme adress:@"目的地"];

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值