接入百度地图授权状态
来源:互联网 发布:淘宝虚拟商品有哪些 编辑:程序博客网 时间:2024/05/16 04:36
参考链接:http://www.jianshu.com/p/f58be9373b6aif ([CLLocationManager locationServicesEnabled] //确定用户的位置服务启用 &&[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) //位置服务是在设置中禁用 { UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"打开定位开关" message:@"定位服务未开启,请进入" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil]; [alert show]; }else{ [self initLocationService]; }
/////////---------------------------------------------------------- 导入框架
Xcode中添加“CoreLocation.framework”
导入主头文件
#import <CoreLocation/CoreLocation.h>
声明管理器和代理
@interface ViewController ()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager* locationManager; @end
初始化管理器
self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
开启定位服务,需要定位时调用findMe方法:
- (void)findMe { /** 由于IOS8中定位的授权机制改变 需要进行手动授权 * 获取授权认证,两个方法: * [self.locationManager requestWhenInUseAuthorization]; * [self.locationManager requestAlwaysAuthorization]; */ if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { NSLog(@"requestAlwaysAuthorization"); [self.locationManager requestAlwaysAuthorization]; } //开始定位,不断调用其代理方法 [self.locationManager startUpdatingLocation]; NSLog(@"start gps"); }
代理设置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // 1.获取用户位置的对象 CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude); // 2.停止定位 [manager stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 } }
注意事项
iOS8中定位服务的变化:CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示,解决方法如下,详见1 详见2
如果需要仅在前台定位,你在调用startUpdatingLocation 前需要调用requestWhenInUseAuthorization(见设置步骤6) 如果需要在前后台定位,你在调用startUpdatingLocation 前需要调用requestAlwaysAuthorization 在plist文件中添加NSLocationWhenInUseUsageDescription或(与)NSLocationAlwaysUsageDescription字段: 找到info.plist文件->右击->Open As->Source Code->在尾部的</dict>标签之前添加以下一个或两个: <key>NSLocationWhenInUseUsageDescription</key><string>需要定位</string> <key>NSLocationAlwaysUsageDescription</key><string>需要定位</string>
用户隐私的保护
从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权:要想获得用户的位置,想访问用户的通讯录、日历、相机、相册等;当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权。 可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description) 从iOS 8开始,用户定位分两种情况 总是使用用户位置:NSLocationAlwaysUsageDescription 使用应用时定位:NSLocationWhenInUseDescription 当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权 /** *打开定位服务 *需要在info.plist文件中添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription): *NSLocationWhenInUseUsageDescription 允许在前台使用时获取GPS的描述 *NSLocationAlwaysUsageDescription 允许永远可获取GPS的描述
获取当前软件的定位服务状态
if ([CLLocationManager locationServicesEnabled] //确定用户的位置服务启用 &&[CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied) //位置服务是在设置中禁用 { }
代码:调用startLocation方法即可
#pragma mark Location and Delegate - (void)startLocation { self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; /** 由于IOS8中定位的授权机制改变 需要进行手动授权 * 获取授权认证,两个方法: * [self.locationManager requestWhenInUseAuthorization]; * [self.locationManager requestAlwaysAuthorization]; */ if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { NSLog(@"requestWhenInUseAuthorization"); // [self.locationManager requestWhenInUseAuthorization]; [self.locationManager requestAlwaysAuthorization]; } //开始定位,不断调用其代理方法 [self.locationManager startUpdatingLocation]; NSLog(@"start gps"); } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // 1.获取用户位置的对象 CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coordinate = location.coordinate; NSLog(@"纬度:%f 经度:%f", coordinate.latitude, coordinate.longitude); self.longitute = [NSNumber numberWithDouble:coordinate.longitude]; self.latitude = [NSNumber numberWithDouble:coordinate.latitude]; // 2.停止定位 [manager stopUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if (error.code == kCLErrorDenied) { // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 } }
阅读全文
0 0
- 接入百度地图授权状态
- 接入百度地图
- 百度地图接入注意
- Android 接入百度地图
- android 百度地图支付接入
- ios百度地图接入流程
- Unity接入百度地图SDK
- Unity接入百度地图SDK
- 使用ionic2接入百度地图
- php 百度地图api接入
- 百度地图时好时坏,授权出错。
- 判断地图定位授权状态
- ArcGIS Server Flex API接入百度地图
- Android百度地图API-快速接入
- 项目接入百度地图SDK环境配置
- 在网页中接入百度地图
- Hello BaiduMap!百度地图Android SDK接入
- 记录百度地图接入的方法
- XML学习---XML文档类型定义~DTD学习
- REMOTE HOST IDENTIFICATION HAS CHANGED 问题解决
- 欢迎使用CSDN-markdown编辑器
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- 算法笔记(一)
- 接入百度地图授权状态
- LeetCode:Validate Binary Search Tree
- JAXB 实现java对象与xml之间互相转换
- 数位DP
- php提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
- WordPress主题站整站打包源码安装方法
- 文章标题
- Maven编译报错sun.misc.BASE64Decoder是Sun的专用API,可能会在未来版本删除
- Android自定义view 实现半圆环