热门标签 | 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];

}



推荐阅读
  • 在iOS6之后,不再使用谷歌地图了,而是使用苹果自己的地图,但是API编程接口没有太大的变化。开发人员不需要再学习很多新东西就能开发地图应用,这是负责任的做法。因此本节介绍的内容也同样适用于iOS5 ... [详细]
  • IOS9之当前位置定位
    2019独角兽企业重金招聘Python工程师标准#import*.h文件中导入以下两个框架*#import ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了iOS核心笔记—CoreLocation框架-基础相关的知识,希望对你有一定的参考价值。1、 ... [详细]
  • IOS开发之百度地图API环境搭建 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 添加#import头文件倒入mapkit.framework库mapkit.framework是属于ui,可以在故事版上添加mkmap ... [详细]
  • IOS笔记汇总为了方便开发者开发出强大的功能,苹果提供了各种各样的框架IOS属性IOS基础属性导入依赖propertyNSStringNSDictionaryNSAr ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • iPhoneSDK提供了三个类来管理位置信息:CLLocationCLLocationManager和CLLHeading(不常用)。除了使用GPS来获取当前的位置信息外,iPho ... [详细]
  • Ihavemanyannotationsinamapview(withrightCalloutAccessorybuttons).Thebuttonwillperforma ... [详细]
  • iOS 百度地图开发集成使用 ... [详细]
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社区 版权所有