#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *manager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1 首先导入框架
// 2 导入头文件
// 3 创建位置管理器
CLLocationManager *manager = [[CLLocationManager alloc]init];
self.manager = manager;
// 4 进行授权
[manager requestAlwaysAuthorization]; // 一直授权
// [manager requestWhenInUseAuthorization]; // 使用的时候进行授权
// 5 开始定位
[manager startUpdatingLocation]; // 开始定位
// ios9 以后, 有个后台定位
// manager.allowsBackgroundLocationUpdates = YES;
// 设置精准度
manager.desiredAccuracy = kCLLocationAccuracyBest;
// 设置精准位置, 100米范围
manager.distanceFilter = 100;
// 6 设置代理
manager.delegate = self;
// 计算两者之间的距离
[self comparedistace];
}
#pragma mark 代理方法
/**
* 完成定位回来到此方法
*
* @param manager 位置管理器
* @param locations 位置的一些信息
*/
// 不断的被调用, 如果只想得到一次, 就停止更新
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
// 获取一个位置信息
CLLocation *location = locations.lastObject;
NSLog(@"%f--%f",location.coordinate.latitude, location.coordinate.longitude);
// [self.manager stopUpdatingLocation];
}
// 计算两者之间距离
- (void)comparedistace{
// 首先先创建两个location
CLLocation *location1 = [[CLLocation alloc]initWithLatitude:41.0 longitude:116.1];
CLLocation *location2 = [[CLLocation alloc]initWithLatitude:42.0 longitude:116.2];
double distance = [location1 distanceFromLocation:location2];
NSLog(@"%f",distance);
}