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

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

import <CoreLocation/CoreLocation.h>

import <MapKit/MapKit.h>

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

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


WX20180119-142938@2x.png

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


WX20180119-143011@2x.png

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];
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268