iOS开发——项目中的地图跳转(苹果地图,百度地图,高德地图)

本文介绍了一个iOS应用中实现的地图导航功能,通过UIActionSheet让用户选择不同的地图应用进行导航,并提供了苹果地图、百度地图和高德地图的支持。若未安装地图应用,则默认使用苹果地图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目中遇到一个小需求,就是点击地图上的大头针进行导航,如果手机中装了多种地图软件,就用提示框显示出来,如果没有装地图软件,就默认跳转到苹果自带的地图进行导航,代码如下:
//导航按钮
- (void)guideBtnAction:(UIButton *)button
{
    //创建时不指定按钮
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
    NSArray *mapArray = @[@"http://maps.apple.com/",@"baidumap://map/",@"iosamap://"];
    NSArray *mapNameArray = @[@"苹果地图",@"百度地图",@"高德地图"];
    for (int i = 0; i < 3; i ++) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:mapArray[i]]]) {
            [sheet addButtonWithTitle:mapNameArray[i]];
        }
    }
    
    //添加取消按钮
    [sheet addButtonWithTitle:@"取消"];
    sheet.delegate = self;
    sheet.cancelButtonIndex = sheet.numberOfButtons - 1;
    [sheet showInView:self.view.window];
}

接下来是响应提示框的按钮事件:


#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        [actionSheet dismissWithClickedButtonIndex:actionSheet.numberOfButtons - 1 animated:YES];
        return;
    }
    
    if (actionSheet.numberOfButtons == 4) {
        switch (buttonIndex) {
            case 0:
                [self openNativeAppleMapWithTag];
                break;
            case 1:
                [self openNativeNaviBaiDu];
                break;
            case 2:
                [self openNativiGaoDe];
                break;
            default:
                break;
        }
    }else if (actionSheet.numberOfButtons == 3){
        switch (buttonIndex) {
            case 0:
                [self openNativeAppleMapWithTag];
                break;
            case 1:{
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
                    [self openNativiGaoDe];
                }
                if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {
                    [self openNativeNaviBaiDu];
                }
            }
                break;
            default:
                break;
        }
    }else{
        switch (buttonIndex) {
            case 0:
                [self openNativeAppleMapWithTag];
                break;
                
            default:
                break;
        }
    }
    
}


//调用高德地图客户端
- (void)openNativiGaoDe
{
    //起点
    CLLocation *location = [[CLLocation alloc]initWithLatitude:[_latitude floatValue] longitude:[_longitude floatValue]];
    CLLocationCoordinate2D coor = location.coordinate;
    
    //终点
    CLLocation *location2 = [[CLLocation alloc]initWithLatitude:[self.pointModel.lat floatValue] longitude:[self.pointModel.lng floatValue]];
    CLLocationCoordinate2D coor2 = location2.coordinate;
    NSString *url = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=%lf&slon=%lf&sname=我的位置&did=BGVIS2&dlat=%lf&dlon=%lf&dname=%@&dev=0&m=0",coor.latitude,coor.longitude, coor2.latitude - 0.00328,coor2.longitude - 0.01185,self.pointModel.stationname] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

//调用百度地图客户端导航
- (void)openNativeNaviBaiDu
{
    NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving";
    NSString *urlString = [[NSString stringWithFormat:baiduParameterFormat,[_latitude floatValue],[_longitude floatValue],[self.pointModel.lat floatValue],[self.pointModel.lng floatValue]]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString]];
}

//调用苹果地图
- (void)openNativeAppleMapWithTag
{
    //起点
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];;
    CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake([self.pointModel.lat floatValue] - 0.00328, [self.pointModel.lng floatValue] - 0.01185);
    //终点
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
    toLocation.name = [NSString stringWithFormat:@"%@",self.pointModel.stationname];
    //默认驾车
    [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值