iOS开发笔记 调起本地地图导航(百度、高德、腾讯、苹果自带)

1476970-49865b09e714c54e.jpeg
地图

从自己的APP跳转到用户本地的APP进行导航。首先,要先查看用户都安装了哪些地图类APP.

下面分3种情况进行分析:
1、用户没有安装第三方的地图,只有苹果自带的地图应用。
2、用户安装一款第三方地图应用。
3、用户安装了超过一款地图应用。
遇到第1、2情况直接跳转应用。第三种情况,需要弹出选项,让用户自主选择。

代码:

//查看线路
- (void)clickLine:(UIButton *)sender{
    NSMutableArray *mapArr = [NSMutableArray arrayWithCapacity:0];
    
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
        [mapArr addObject:@"百度地图"];
    }
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
        [mapArr addObject:@"高德地图"];
    }
    if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
        [mapArr addObject:@"腾讯地图"];
    }
    
    if (mapArr.count == 1) {
        [self JumpToMap:mapArr[0]];
    }else if(mapArr.count > 0){
        UIAlertController *mapAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        for (NSString *mapName in mapArr) {
            UIAlertAction *Action = [UIAlertAction actionWithTitle:mapName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self JumpToMap:action.title];
            }];
            [mapAlert addAction:Action];
        }
        
        //取消
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [mapAlert addAction:cancelAction];
        
        [self presentViewController:mapAlert animated:YES completion:^{
            
        }];
    }else{
        //使用自带地图
        [self JumpToMap:@"苹果地图"];
    }
}

分流函数:

//选择地图
- (void)JumpToMap:(NSString *)mapName{
    if ([mapName isEqualToString:@"苹果地图"]) {
        [self appleMap];
    }else if ([mapName isEqualToString:@"百度地图"]){
        [self BaiduMap];
    }else if ([mapName isEqualToString:@"高德地图"]){
        [self iosMap];
    }else if ([mapName isEqualToString:@"腾讯地图"]){
        [self qqMap];
    }
}
备注:如果兼容ios10以下的应用,跳转请做好适配,ios10以下系统请使用:
- openURL:(NSURL*)url

百度地图

百度地图官方文档

//百度地图
- (void)BaiduMap{
    float shopLat = 百度坐标;
    float shoplng = 百度坐标;
    
    NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&mode=transit&coord_type= bd09ll",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    if (shopLat != 0 && shoplng != 0) {
        urlString = [NSString stringWithFormat:@"%@&destination=latlng:%f,%f|name:%@", urlString, shopLat, shoplng, @"目标地址,你可以自行替换"];
    }else{
        urlString = [NSString stringWithFormat:@"%@&destination=%@|name:%@",urlString, _orderModel.addressStr,_orderModel.addressStr];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

高德地图

高德地图官方文档

//高德地图
- (void)iosMap{
    CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐标, 百度坐标);
    
    float shopLat = gcj02Coord.latitude;
    float shoplng = gcj02Coord.longitude;
    
    NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=jikexiue&backScheme=jkxe&slat=%f&slon=%f&sname=我的位置&dev=1&t=1",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    if (shopLat != 0 && shoplng != 0) {
        urlString = [NSString stringWithFormat:@"%@&dlat=%f&dlon=%f&dname=%@", urlString, shopLat, shoplng ,_orderModel.addressStr];
    }else{
        urlString = [NSString stringWithFormat:@"%@&dname=%@",urlString, _orderModel.addressStr];
    }
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

腾讯地图

腾讯地图官方文档

//腾讯地图
- (void)qqMap{
    
    CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐标, 百度坐标);
    
    float shopLat = gcj02Coord.latitude;
    float shoplng = gcj02Coord.longitude;
    
    NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=bus&fromcoord=%f,%f&from=我的位置&referer=jikexiu",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
    
    urlString = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",urlString, shopLat, shoplng, _orderModel.addressStr];
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
        
    }];
}

苹果原生地图

1、添加库MapKit.framework
2、引入: #import <MapKit/MapKit.h>

//苹果原生地图
- (void)appleMap{
    CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake(_orderModel.lat, _orderModel.lng);
    
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    currentLocation.name = @"我的位置";
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
    toLocation.name = [NSString stringWithFormat:@"%@",_orderModel.addressStr];
    
    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}

猜你喜欢

转载自blog.csdn.net/weixin_34235135/article/details/87161744
今日推荐