iOS显示定位权限和打开第三方地图

一:定位权限
1.导入CoreLocation,MapKit库。
2.在需要弹出位置授权的界面导入头文件

import

import

3.info.plist文件添加3个授权
a. NSLocationWhenInUseUsageDescription (应用使用期间),
b. NSLocationAlwaysUsageDescription(始终允许),
c. NSLocationAlwaysAndWhenInUseUsageDescription(始终允许,iOS11新增)
在iOS11时,NSLocationAlwaysAndWhenInUseUsageDescription表示始终允许,NSLocationAlwaysUsageDescription在功能上被降级为为“应用使用期间”。

如果a,b两项添加到plist里,授权提示有2个选择项


如果a,b,c 全部添加到plist里,授权提示有3个选择项


4.跳转到地图界面前,进行位置权限判断,只有允许了定位才能跳转界面。
-(void)clickToMapVC{

 //确定用户的位置服务是否启用,位置服务在设置中是否被禁用
BOOL enable      =[CLLocationManager locationServicesEnabled];
NSInteger status =[CLLocationManager authorizationStatus];
if(  !enable || status< 2){
    //尚未授权位置权限
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8)
    {
        //系统位置授权弹窗
        _locationManager =[[CLLocationManager alloc]init];
        [_locationManager requestAlwaysAuthorization];
        [_locationManager requestWhenInUseAuthorization];
    }
}else{
    if (status == kCLAuthorizationStatusDenied) {
        //拒绝使用位置
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:nil message:@"地点功能需要开启位置授权" delegate:self cancelButtonTitle:@"暂不设置" otherButtonTitles:@"现在去设置", nil];
        [alterView show];
    }else{
        //允许使用位置
        MapLocationVC *mapVC =[[MapLocationVC alloc]init];
        mapVC.fromComment =YES;
        mapVC.delegate =self;
        if ([self.delegate respondsToSelector:@selector(presentVC:)]) {
            [self.delegate presentVC:mapVC];
        }
    }
}

}

二:打开第三方地图
//导航到目的地,endLocation为目的地经纬度
-(void)navThirdMapWithLocation:(CLLocationCoordinate2D)endLocation

NSMutableArray *mapsA = [NSMutableArray array];
//苹果原生地图方法和其他不一样
NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
iosMapDic[@"title"] = @"苹果地图";
iosMapDic[@"url"]   = [NSString stringWithFormat:@"%f-%f",endLocation.latitude,endLocation.longitude];
[mapsA addObject:iosMapDic];

//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
    NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
    gaodeMapDic[@"title"] = @"高德地图";
    NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    gaodeMapDic[@"url"] = urlString;
    [mapsA addObject:gaodeMapDic];
}

//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
    NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
    qqMapDic[@"title"] = @"腾讯地图";
    CLLocationCoordinate2D afterLocation =endLocation;
    NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",afterLocation.latitude, afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    qqMapDic[@"url"] = urlString;
    [mapsA addObject:qqMapDic];
}

//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
    NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
    baiduMapDic[@"title"] = @"百度地图";
    //坐标转换
    CLLocationCoordinate2D afterLocation =endLocation;
    NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=北京哈&mode=driving&coord_type=gcj02",afterLocation.latitude,afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    baiduMapDic[@"url"] = urlString;
    [mapsA addObject:baiduMapDic];
}

//谷歌地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
    NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
    googleMapDic[@"title"] = @"谷歌地图";
    CLLocationCoordinate2D afterLocation =endLocation;
    NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",afterLocation.latitude, afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    googleMapDic[@"url"] = urlString;
    [mapsA addObject:googleMapDic];
}

//手机地图个数判断
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 navAppleMap:urlString];
            }];
            [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];
    
    if ([self.delegate respondsToSelector:@selector(presentVC:)]) {
        [self.delegate presentVC:alertVC];
    }
}else{
    [MBProgressHUD showError:@"未检测到地图应用"];
}

}

//跳转到原生苹果地图

  • (void)navAppleMap:(NSString *)urlString{

    NSArray *location = [urlString componentsSeparatedByString:@"-"];
    CGFloat latitude = [location[0] floatValue];
    CGFloat longitude = [location[1] floatValue];

    //终点坐标转换
    CLLocationCoordinate2D afterLocation =CLLocationCoordinate2DMake(latitude, longitude);
    //用户位置
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    //终点位置
    MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:afterLocation addressDictionary:nil] ];
    //目的地名称
    toLocation.name = @"地图上的点";//endLocation坐标对应的目的地名称
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{
    MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
    MKLaunchOptionsMapTypeKey : [NSNumber numberWithInteger:MKMapTypeStandard],
    MKLaunchOptionsShowsTrafficKey : @(YES)
    };
    [MKMapItem openMapsWithItems:items launchOptions:dic];
    }

你可能感兴趣的:(iOS显示定位权限和打开第三方地图)