热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS开启Google位置服务器和显示定位权限的方法

本文介绍了在iOS开发中如何开启Google位置服务器和显示定位权限的方法,包括导入CoreLocation和MapKit库、在界面导入头文件和在info.plist文件中添加授权等步骤。同时还介绍了iOS11中NSLocationAlwaysAndWhenInUseUsageDescription的功能变化。阅读本文可以帮助开发者了解如何在iOS应用中使用Google位置服务器和处理定位权限相关的问题。

一:定位权限

1.导入CoreLocation,MapKit库。

2.在需要弹出位置授权的界面导入头文件

import

import

3.info.plist文件添加3个授权

a. NSLocationWhenInUseUsageDescription (应用使用期间),

b. NSLocationAlwaysUsageDescription(始终允许),

c. NSLocationAlwaysAndWhenInUseUsageDescription(始终允许,iOS11新增)

在iOS11时,NSLocationAlwaysAndWhenInUseUsageDescription表示始终允许,NSLocationAlwaysUsageDescription在功能上被降级为为“应用使用期间”。

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

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

-(void)clickToMapVC{

//确定用户的位置服务是否启用,位置服务在设置中是否被禁用

BOOL enable =[CLLocationManager locationServicesEnabled];

NSInteger status =[CLLocationManager authorizationStatus];

if( !enable || status<2){

//尚未授权位置权限

if ([[UIDevice currentDevice].systemVersion floatValue] >&#61; 8)

{

//系统位置授权弹窗

_locationManager &#61;[[CLLocationManager alloc]init];

[_locationManager requestAlwaysAuthorization];

[_locationManager requestWhenInUseAuthorization];

}

}else{

if (status &#61;&#61; kCLAuthorizationStatusDenied) {

//拒绝使用位置

UIAlertView *alterView &#61; [[UIAlertView alloc] initWithTitle:nil message:&#64;"地点功能需要开启位置授权" delegate:self cancelButtonTitle:&#64;"暂不设置" otherButtonTitles:&#64;"现在去设置", nil];

[alterView show];

}else{

//允许使用位置

MapLocationVC *mapVC &#61;[[MapLocationVC alloc]init];

mapVC.fromComment &#61;YES;

mapVC.delegate &#61;self;

if ([self.delegate respondsToSelector:&#64;selector(presentVC:)]) {

[self.delegate presentVC:mapVC];

}

}

}

}

二&#xff1a;打开第三方地图

//导航到目的地&#xff0c;endLocation为目的地经纬度

-(void)navThirdMapWithLocation:(CLLocationCoordinate2D)endLocation

NSMutableArray *mapsA &#61; [NSMutableArray array];

//苹果原生地图方法和其他不一样

NSMutableDictionary *iosMapDic &#61; [NSMutableDictionary dictionary];

iosMapDic[&#64;"title"] &#61; &#64;"苹果地图";

iosMapDic[&#64;"url"] &#61; [NSString stringWithFormat:&#64;"%f-%f",endLocation.latitude,endLocation.longitude];

[mapsA addObject:iosMapDic];

//高德地图

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:&#64;"iosamap://"]]) {

NSMutableDictionary *gaodeMapDic &#61; [NSMutableDictionary dictionary];

gaodeMapDic[&#64;"title"] &#61; &#64;"高德地图";

NSString *urlString &#61; [[NSString stringWithFormat:&#64;"iosamap://navi?sourceApplication&#61;%&#64;&backScheme&#61;%&#64;&lat&#61;%f&lon&#61;%f&dev&#61;0&style&#61;2",&#64;"导航功能",&#64;"nav123456",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

gaodeMapDic[&#64;"url"] &#61; urlString;

[mapsA addObject:gaodeMapDic];

}

//腾讯地图

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:&#64;"qqmap://"]]) {

NSMutableDictionary *qqMapDic &#61; [NSMutableDictionary dictionary];

qqMapDic[&#64;"title"] &#61; &#64;"腾讯地图";

CLLocationCoordinate2D afterLocation &#61;endLocation;

NSString *urlString &#61; [[NSString stringWithFormat:&#64;"qqmap://map/routeplan?from&#61;我的位置&type&#61;drive&tocoord&#61;%f,%f&to&#61;终点&coord_type&#61;1&policy&#61;0",afterLocation.latitude, afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

qqMapDic[&#64;"url"] &#61; urlString;

[mapsA addObject:qqMapDic];

}

//百度地图

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:&#64;"baidumap://"]]) {

NSMutableDictionary *baiduMapDic &#61; [NSMutableDictionary dictionary];

baiduMapDic[&#64;"title"] &#61; &#64;"百度地图";

//坐标转换

CLLocationCoordinate2D afterLocation &#61;endLocation;

NSString *urlString &#61; [[NSString stringWithFormat:&#64;"baidumap://map/direction?origin&#61;{{我的位置}}&destination&#61;latlng:%f,%f|name&#61;北京哈&mode&#61;driving&coord_type&#61;gcj02",afterLocation.latitude,afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

baiduMapDic[&#64;"url"] &#61; urlString;

[mapsA addObject:baiduMapDic];

}

//谷歌地图

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:&#64;"comgooglemaps://"]]) {

NSMutableDictionary *googleMapDic &#61; [NSMutableDictionary dictionary];

googleMapDic[&#64;"title"] &#61; &#64;"谷歌地图";

CLLocationCoordinate2D afterLocation &#61;endLocation;

NSString *urlString &#61; [[NSString stringWithFormat:&#64;"comgooglemaps://?x-source&#61;%&#64;&x-success&#61;%&#64;&saddr&#61;&daddr&#61;%f,%f&directionsmode&#61;driving",&#64;"导航测试",&#64;"nav123456",afterLocation.latitude, afterLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

googleMapDic[&#64;"url"] &#61; urlString;

[mapsA addObject:googleMapDic];

}

//手机地图个数判断

if (mapsA.count > 0) {

//选择

UIAlertController *alertVC &#61; [UIAlertController alertControllerWithTitle:&#64;"使用导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

NSInteger index &#61; mapsA.count;

for (int i &#61; 0; i

NSString *title &#61; mapsA[i][&#64;"title"];

NSString *urlString &#61; mapsA[i][&#64;"url"];

//苹果原生地图方法

if (i &#61;&#61; 0) {

UIAlertAction *iosAntion &#61; [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

[self navAppleMap:urlString];

}];

[alertVC addAction:iosAntion];

continue;

}

UIAlertAction *action &#61; [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

}];

[alertVC addAction:action];

}

UIAlertAction *cancleAct &#61; [UIAlertAction actionWithTitle:&#64;"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

[alertVC addAction:cancleAct];

if ([self.delegate respondsToSelector:&#64;selector(presentVC:)]) {

[self.delegate presentVC:alertVC];

}

}else{

[MBProgressHUD showError:&#64;"未检测到地图应用"];

}

}

//跳转到原生苹果地图

(void)navAppleMap:(NSString *)urlString{

NSArray *location &#61; [urlString componentsSeparatedByString:&#64;"-"];

CGFloat latitude &#61; [location[0] floatValue];

CGFloat longitude &#61; [location[1] floatValue];

//终点坐标转换

CLLocationCoordinate2D afterLocation &#61;CLLocationCoordinate2DMake(latitude, longitude);

//用户位置

MKMapItem *currentLoc &#61; [MKMapItem mapItemForCurrentLocation];

//终点位置

MKMapItem *toLocation &#61; [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:afterLocation addressDictionary:nil] ];

//目的地名称

toLocation.name &#61; &#64;"地图上的点";//endLocation坐标对应的目的地名称

NSArray *items &#61; &#64;[currentLoc,toLocation];

NSDictionary *dic &#61; &#64;{

MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,

MKLaunchOptionsMapTypeKey : [NSNumber numberWithInteger:MKMapTypeStandard],

MKLaunchOptionsShowsTrafficKey : &#64;(YES)

};

[MKMapItem openMapsWithItems:items launchOptions:dic];

}



推荐阅读
author-avatar
我活该难受G
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有