話不多說直接看代碼吧??
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
{
// 老位置
CLLocation *_oldL;
}
/** 位置管理者 */
@property (nonatomic, strong) CLLocationManager *lM;
@end
@implementation ViewController
#pragma mark - 懶加載
- (CLLocationManager *)lM
{
if (!_lM) {
// 1. 創(chuàng)建位置管理者
_lM = [[CLLocationManager alloc] init];
// 1.1 代理, 通知, block
_lM.delegate = self;
// 每隔多米定位一次
// _lM.distanceFilter = 100;
/**
kCLLocationAccuracyBestForNavigation // 最適合導(dǎo)航
kCLLocationAccuracyBest; // 最好的
kCLLocationAccuracyNearestTenMeters; // 10m
kCLLocationAccuracyHundredMeters; // 100m
kCLLocationAccuracyKilometer; // 1000m
kCLLocationAccuracyThreeKilometers; // 3000m
*/
// 精確度越高, 越耗電, 定位時(shí)間越長
_lM.desiredAccuracy = kCLLocationAccuracyBest;
/** -------iOS8.0+定位適配-------- */
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
// 前臺(tái)定位授權(quán)(默認(rèn)情況下,不可以在后臺(tái)獲取位置, 勾選后臺(tái)模式 location update, 但是 會(huì)出現(xiàn)藍(lán)條)
[_lM requestWhenInUseAuthorization];
// 前后臺(tái)定位授權(quán)(請(qǐng)求永久授權(quán))
// +authorizationStatus != kCLAuthorizationStatusNotDetermined
// 這個(gè)方法不會(huì)有效
// 當(dāng)前的授權(quán)狀態(tài)為前臺(tái)授權(quán)時(shí),此方法也會(huì)有效
// [_lM requestAlwaysAuthorization];
}
// 允許后臺(tái)獲取用戶位置(iOS9.0)
if([[UIDevice currentDevice].systemVersion floatValue] >= 9.0)
{
// 一定要勾選后臺(tái)模式 location updates
_lM.allowsBackgroundLocationUpdates = YES;
}
// if ([_lM respondsToSelector:@selector(requestAlwaysAuthorization)])
// {
// [_lM requestAlwaysAuthorization];
// }
}
return _lM;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.lM startUpdatingLocation];
// CLLocation *l1 = [[CLLocation alloc] initWithLatitude:21.123 longitude:123.456];
// CLLocation *l2 = [[CLLocation alloc] initWithLatitude:22.123 longitude:123.456];
//
// CLLocationDistance distance = [l1 distanceFromLocation:l2];
// NSLog(@"%f", distance);
}
#pragma mark - CLLocationManagerDelegate
/**
* 更新到位置之后調(diào)用
*
* @param manager 位置管理者
* @param locations 位置數(shù)組
* is kind of
*/
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
// NSLog(@"定位到了");
/**
* CLLocation 詳解
* coordinate : 經(jīng)緯度
* altitude : 海拔
* course : 航向
* speed ; 速度
*/
CLLocation *location = [locations lastObject];
// NSLog(@"%@", location);
/**
* 場(chǎng)景演示:打印當(dāng)前用戶的行走方向,偏離角度以及對(duì)應(yīng)的行走距離,
例如:”北偏東 30度 方向,移動(dòng)了8米”
*/
// 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 = 0;
angle = (int)location.course % 90;
// 代表正方向
if (angle == 0) {
NSRange range = NSMakeRange(0, 1);
angleStr = [NSString stringWithFormat:@"正%@", [angleStr substringWithRange:range]];
}
// 3.移動(dòng)多少米
double distance = 0;
if(_oldL)
{
distance = [location distanceFromLocation:_oldL];
}
_oldL = location;
// 4. 拼串 打印
// 例如:”北偏東 30度 方向,移動(dòng)了8米”
NSString *noticeStr = [NSString stringWithFormat:@"%@%zd方向, 移動(dòng)了%f米", angleStr, angle, distance];
NSLog(@"%@", noticeStr);
}
/**
* 授權(quán)狀態(tài)發(fā)生改變時(shí)調(diào)用
*
* @param manager 位置管理者
* @param status 狀態(tài)
*/
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
// 用戶還未決定
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用戶還未決定");
break;
}
// 問受限
case kCLAuthorizationStatusRestricted:
{
NSLog(@"訪問受限");
break;
}
// 定位關(guān)閉時(shí)和對(duì)此APP授權(quán)為never時(shí)調(diào)用
case kCLAuthorizationStatusDenied:
{
// 定位是否可用(是否支持定位或者定位是否開啟)
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位開啟,但被拒");
}else
{
NSLog(@"定位關(guān)閉,不可用");
}
// NSLog(@"被拒");
break;
}
// 獲取前后臺(tái)定位授權(quán)
case kCLAuthorizationStatusAuthorizedAlways:
// case kCLAuthorizationStatusAuthorized: // 失效,不建議使用
{
NSLog(@"獲取前后臺(tái)定位授權(quán)");
break;
}
// 獲得前臺(tái)定位授權(quán)
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"獲得前臺(tái)定位授權(quán)");
break;
}
default:
break;
}
}
// 定位失敗
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"定位失敗");
}
@end