CoreLocation框架使用前提
导入主头文件: CoreLocation/CoreLocation.h
CoreLocation框架使用须知:
- CoreLocation框架中所有数据类型的前缀都是CL
- CoreLocation中使用CLLocationManager对象来做用户定位
CLLocationManager的常用操作
开始用户定位
- (void)startUpdatingLocation;
停止用户定位
- (void) stopUpdatingLocation;
当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
- (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray )locations;
locations参数里面装着CLLocation对象
CLLocation的属性
CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
经纬度
@property(readonly, nonatomic) CLLocationDistance altitude;
海拔
@property(readonly, nonatomic) CLLocationDirection course;
路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)
@property(readonly, nonatomic) CLLocationSpeed speed;
行走速度(单位是m/s)
用- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location方法可以计算2个位置之间的距离
CLLocationManager的属性
@property(assign, nonatomic) CLLocationDistance distanceFilter;
每隔多少米定位一次
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
定位精确度(越精确就越耗电)
CLLocationCoordinate2D
CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
typedef struct {
CLLocationDegrees latitude; // 纬度
CLLocationDegrees longitude; // 经度
} CLLocationCoordinate2D;
一般用CLLocationCoordinate2DMake函数来创建CLLocationCoordinate2D
用户隐私的保护
从iOS 6开始,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权
要想获得用户的位置
想访问用户的通讯录、日历、相机、相册等等
当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权
开发者可以在Info.plist中设置NSLocationUsageDescription说明定位的目的(Privacy - Location Usage Description)
一旦用户选择了“Don’t Allow”,意味着你的应用以后就无法使用定位功能
为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
CLLocationManager有个类方法可以判断当前应用的定位功能是否可用
+ (BOOL)locationServicesEnabled;
示例代码:
引入头文件并声明属性:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>// 倒入地图框架
@interface ViewController ()<CLLocationManagerDelegate>// 遵守协议
@property (nonatomic, strong) CLLocationManager *manager;// 定位管理类对象(定位用户的位置)
@end
懒加载创建属性:
- (CLLocationManager *)manager{ // 1.创建位置管理器(定位用户的位置)
if (_manager == nil) {
_manager = [[CLLocationManager alloc]init];
self.mnager.delegate = self; // 设置代理
}
return _manager;
}
在 - (void)viewDidLoad{}方法里熟悉代码,开启定位,设置相关属性
- (void)viewDidLoad {
[super viewDidLoad];
// 请求用户是否允许使用定位功能(先去info.plist文件里配置(NSLocationAlwaysUsageDescription)
// NSLocationWhenInUsageDescription/NSLocationAlwaysUsageDescription(两种定位方式)
[self.manager requestAlwaysAuthorization];
if ([CLLocationManager locationServicesEnabled]) {// 如果定位服务能用
[self.manager startUpdatingLocation]; // 开启定位
self.manager.distanceFilter = 100;// 定位过滤 每隔多少米定位一次(不需要频繁定位时)
// 定位精度,精度越高越耗电
self.manager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
} else {// 不能定位用户位置
// 告诉用户检查网络状况 或者 提醒用户开启定位功能
}
[self countDistance];// 调用获取两点距离的方法
}
获取两点间距离
- (void)countDistance
{
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:40 longitude:116];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:41 longitude:116];
CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
NSLog(@"(%@)和(%@)的距离:%f", loc1, loc2, distance);
}
定位到用户位置时调用此代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
// 数组里面存放的是CLLocation对象, 一个CLLocation对象就代表一个位置
CLLocation *location = locations.lastObject;
// 纬度:location.coordinate.latitude
// 经度:location.coordinate.longitude
NSLog(@"纬度=%f, 经度=%f", location.coordinate.latitude, location.coordinate.longitude);
// 停止更新位置(不用定位服务时,应当马上停止定位服务,定位服务时比较好点的)
[self.manager stopUpdatingLocation];
}