#import "ViewController.h"// 0. 導(dǎo)入頭文件#import <CoreLocation/CoreLocation.h>@interface ViewController () <CLLocationManagerDelegate>/** 位置管理者 */@PRoperty (nonatomic, strong) CLLocationManager *manager;@end@implementation ViewController- (CLLocationManager *)manager{ if (!_manager) { // 1. 創(chuàng)建CLLocationManager _manager = [[CLLocationManager alloc] init]; // 2. 設(shè)置代理 _manager.delegate = self; // 每隔多少米定位一次 _manager.distanceFilter = 100; /** kCLLocationAccuracyBestForNavigation // 最適合導(dǎo)航 kCLLocationAccuracyBest; // 最好的精確度,僅次于kCLLocationAccuracyBestForNavigation kCLLocationAccuracyNearestTenMeters; // 附近10m kCLLocationAccuracyHundredMeters; // 100m kCLLocationAccuracyKilometer; // 1千米 kCLLocationAccuracyThreeKilometers; // 3千米 */ // 設(shè)置定位精確度 // 并不是精確度越高就越好,精確度越高,就越耗性能,越費(fèi)電,要根據(jù)需求來設(shè)置精確度 _manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; } return _manager;}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 3.開始更新地址 // 此處重點(diǎn):一定要在info.plist文件中加上key:Privacy - Location Usage Description,value可以隨便寫 [self.manager startUpdatingLocation];}#pragma mark - 實(shí)現(xiàn)CLLocationManagerDelegate代理方法// 4.實(shí)現(xiàn)代理方法- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations lastObject]; NSLog(@"%@ -- %f", location.description, location.speed);}@end
解決方案:
(1)調(diào)用iOS 8.0的API,主動請求用戶授權(quán)
(2)務(wù)必在info.plist文件中配置對應(yīng)的鍵值, 否則以上請求授權(quán)的方法不生效
NSLocationAlwaysUsageDescription : 允許在前后臺獲取GPS的描述
NSLocationWhenInUseDescription : 允許在前臺獲取GPS的描述
#import "ViewController.h"#import <CoreLocation/CoreLocation.h>@interface ViewController () <CLLocationManagerDelegate>/** 位置管理者 */@property (nonatomic, strong) CLLocationManager *manager;@end@implementation ViewController- (CLLocationManager *)manager{ if (_manager == nil) { _manager = [[CLLocationManager alloc] init]; _manager.delegate = self; // 采取前臺定位 // 1. 需要在info.plist文件中添加key:NSLocationWhenInUseUsageDescription // 2. 如果需要在后臺繼續(xù)定位,需要勾選后臺模式,當(dāng)程序進(jìn)入后臺后,會在最上方出現(xiàn)一個(gè)藍(lán)條// [_manager requestWhenInUseAuthorization]; // 采用前后臺定位 // 1. 需要在info.plist文件中添加NSLocationAlwaysUsageDescription這個(gè)key // 2. 這種模式的后臺定位,不需要勾選后臺模式,也不會出現(xiàn)藍(lán)條 [_manager requestAlwaysAuthorization]; // requestWhenInUseAuthorization和requestAlwaysAuthorization請求同時(shí)存在: // 1. requestWhenInUseAuthorization請求在前,會先彈出前臺授權(quán)描述,第二次啟動程序的時(shí)候,還會彈出前后臺授權(quán)描述 // 2. requestAlwaysAuthorization在前,只會彈出前后臺授權(quán)描述,不會彈出前臺授權(quán)描述. } return _manager;}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.manager startUpdatingLocation];}#pragma mark - <CLLocationManagerDelegate>/** * 更新定位 * * @param manager 位置管理器 * @param locations 定位的位置數(shù)組 */- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ NSLog(@"定位了");} _manager.allowsBackgroundLocationUpdates = YES;iOS 9.0 可以單次請求用戶位置:-(void)requestLocation
- (void)requestLocation-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations // 成功調(diào)用-(void)locationManager:(nonnull CLLocationManager *)manager didFailWithError:(nonnull NSError *)error // 失敗調(diào)用/** * 代理方法: 更新定位 * * @param manager 位置管理器 * @param locations 定位的位置數(shù)組 */- (void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations{ // CLLocation常用屬性 // coordinate (當(dāng)前位置所在的經(jīng)緯度) // altitude (海拔) // speed (當(dāng)前速度) // course (航向) // -distanceFromLocation (獲取兩個(gè)位置之間的直線物理距離) CLLocation *location = [locations lastObject]; // 1. 獲取偏向角度 NSString *angleStr = nil; switch ((int)location.course / 90) { case 0: angleStr = @"北偏東"; break; case 1: angleStr = @"東偏南"; break; case 2: angleStr = @"南偏西"; break; case 3: angleStr = @"西偏北"; break; default: angleStr = @"未知位置"; break; } // 2. 偏移角度 NSInteger angle = (int)location.course % 90; if (angle == 0) { // 表示正方向 angleStr = [angleStr substringWithRange:NSMakeRange(0, 1)]; } // 3. 移動了多少米 double distance = 0; if (_oldLocation) { distance = [location distanceFromLocation:_oldLocation]; } _oldLocation = location; // 4. 打印 NSString *locationStr = [NSString stringWithFormat:@"%@%zd度方向,移動了%.2fm", angleStr, angle, distance]; NSLog(@"%@", locationStr);} kCLAuthorizationStatusNotDetermined = 0, // 用戶未決定 kCLAuthorizationStatusRestricted, // 受限制 kCLAuthorizationStatusDenied, // 拒絕 kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0), // 永久授權(quán) kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0), // APP使用的時(shí)候授權(quán)新聞熱點(diǎn)
疑難解答
圖片精選