iOS经典讲解之地图定位请求位置信息时出现的问题
来源:互联网 发布:mac 获取当前文件路径 编辑:程序博客网 时间:2024/06/05 00:40
地图定位请求位置信息在iOS8之后新增两个方法:
- (void)requestWhenInUseAuthorization
- (void)requestAlwaysAuthorization,
但是在使用这两个方法的时候需要手动在Info.plist文件加两个字段,
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。
系统给出了说明:
/* If the NSLocationWhenInUseUsageDescription key is not specified in your * Info.plist, this method will do nothing, as your app will be assumed not * to support WhenInUse authorization. */- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);/* If the NSLocationAlwaysUsageDescription key is not specified in your * Info.plist, this method will do nothing, as your app will be assumed not * to support Always authorization. */- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
示例图片如下:
这样添加代码就可以了:
#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController () <CLLocationManagerDelegate>@property (nonatomic, strong) CLLocationManager *locationManager;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 初始化 self.locationManager = [[CLLocationManager alloc] init]; // 设置代理 self.locationManager.delegate = self; // 定位精度 self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 设置多少米更新一次距离 self.locationManager.distanceFilter = 100; // 什么时候请求位置信息 注意: If the NSLocationAlwaysUsageDescription key is not specified in your Info.plist, this method will do nothing, as your app will be assumed not to support Always authorization. 需要设置plist文件添加NSLocationAlwaysUsageDescription为key即可 [self.locationManager requestAlwaysAuthorization]; // 开始请求位置信息 [self.locationManager startUpdatingLocation]; }#pragma mark -- 代理方法//定位成功时代理方法-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations firstObject]; NSLog(@"%@", location); }// 定位失败- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ // 错误信息描述 NSLog(@"%@", [error localizedDescription]);}@end
接下来就可以获取位置信息了,出现如下提示,说明请求成功了:
0 0
- iOS经典讲解之地图定位请求位置信息时出现的问题
- 我的高德地图之定位,Marker,位置信息。
- IOS 地图位置定位
- IOS 地图位置定位
- iOS经典讲解之百度地图的使用及案例(iOS地图SDK)
- iOS经典讲解之百度地图的使用及案例(iOS地图SDK)
- Android移动开发-通过自定义算法代码来纠偏地图由GPS定位到的经纬度在地图上显示或解析位置时出现偏移的问题
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- iOS经典讲解之UIButton改变图片和文字的位置
- iOS 根据crash报告定位到代码中具体出现问题的位置
- ios 地图与定位(用户位置)
- IOS 定位 提示信息一闪而过 的问题
- iOS开发之地图定位
- iOS之地图与定位
- 值得深思的连续赋值--赋值运算符运算顺序
- java解析XML配置文件及log4j开源日志系统
- 第三章:数据结构与算法javascript描述: 列表
- 匿名内部类的总结
- ZOJ 3822 Domination [概率DP]
- iOS经典讲解之地图定位请求位置信息时出现的问题
- 【解决】该任务映像已损坏或已篡改。(异常来自HRESULT:0x80041321)
- 将外部jar包一同导出到自己的jar包
- springMVC解决跨域请求
- iOS-面试助攻(二)
- BJT比CMOS
- 【转】程序的内存分配之堆和栈的区别
- 马哥运维笔记----6
- 2014acm亚洲赛总结