iOS 定位功能的实现详解
来源:互联网 发布:extend软件 编辑:程序博客网 时间:2024/04/29 01:09
导入框架
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
- iOS 定位功能的实现详解
- iOS 定位功能的实现
- iOS定位功能实现
- iOS中定位功能的实现
- [iOS菜鸟的学习之路]定位功能的实现
- iOS应用后台运行GPS定位功能的实现
- iOS应用后台运行GPS定位功能的实现
- iOS地图及定位功能基本实现的详尽描述
- iOS - 定位功能/获取当前位置信息的实现
- iOS开发中实现后台定位功能
- android 定位功能的实现
- ios 定位新功能----在程序中实现定位功能
- ios 定位新功能----在程序中实现定位功能
- ios系统地图的定位功能
- iOS地图定位功能的实现(解决代理方法不执行问题,解决默认定位苹果总部问题)
- iOS 实时定位功能
- iOS Location 定位功能
- 定位功能iOS SDK
- View.onMeasure()
- Intelx86使用的寄存器
- gulp举例
- VBA_批量调整图片宽度
- 给linux添加一个系统调用(linux-4.10.1)
- iOS 定位功能的实现详解
- OC的内存管理
- leetcodeOJ 62. Unique Paths
- 答题卡识别软件
- BZOJ P1497[NOI2006]最大获利
- VS2013 无法使用'inet_ntoa'
- Android SEAndroid
- C++ string对象的学习与整理
- angularjs 指令"directive"举例