应用场景
- 导航
- 电商APP,获取用户所在城市
- 数据采集用户信息(例如:统计app的使用分布)
- 查找周边(周边好友,周边商家等等)
定位的省电模式
- 不需要获取用户位置时,一定要光比定位服务
- 如果能够满足项目需求,极可能的使用“监听显著位置变化”定位服务(打车app)
- 如果可以,尽可能使用低精度的desiredAccuracy
- 如果是数据采集(一般都是周期性的去轮询用户位置),在轮询周期间一定要关闭定位
iOS8.0之前的定位
- 前台定位
// 1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
// 2. 创建CLLcationManager对象,并设置代理
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
// 3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
// 4. 实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
- 后台定位
-
- 在前台定位基础上,勾选后台模式update locations
-
iOS8.0+ 至 iOS9.0之前的定位
- 前台定位
// 1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
// 2. 创建CLLcationManager对象,并设置代理 * 请求前台定位授权,并配置KEY *
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
// 当时ios8.0 到 ios9.0之间的时候
/* (1).前台定位:需要子啊info.plist中配置:NSLocationWhenInUseUsageDescription
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[_locationM requestWhenInUseAuthorization];
}*/
// (2).前后台定位:需要子啊info.plist中配置:NSLocationAlwaysUsageDescription
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[_locationM requestAlwaysAuthorization];
}
// 3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
// 4. 实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
-
后台定位
- 方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations
- 注意:如果APP处于后台,则会出现蓝条
-
各种授权状态讲解
#pragma mark -CLLocationManagerDelegate
// 如果授权状态发生变化时,调用
// status : 当前的授权状态
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户未决定");
break;
}
case kCLAuthorizationStatusRestricted:
{
NSLog(@"受限制");
break;
}
case kCLAuthorizationStatusDenied:
{
// 判断当前设备是否支持定位, 并且定位服务是否开启()
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"定位开启,被拒绝");
// ios8,0-之前 需要截图提醒引导用户
// iOS8.0+之后,直接跳到设置界面
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
}else
{
NSLog(@"定位服务关闭");
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"前后台定位授权");
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"前台定位授权");
break;
}
default:
break;
}
}
iOS9.0 定位补充
- 前台定位
// 1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
// 2. 创建CLLcationManager对象,并设置代理 * 请求前台定位授权,并配置KEY *
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[_locationM requestWhenInUseAuthorization];
}
// 3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
[_locationM startUpdatingLocation];
// 4. 实现代理方法,接收位置参数
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
- 后台定位
// 方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图) 并且,调用以下方法,设置允许后台定位
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
_locationM.allowsBackgroundLocationUpdates = YES;
}
** 注意:如果APP处于后台,则会出现蓝条 **
// 方案二:请求前后台定位授权,并配置KEY
[_locationM requestAlwaysAuthorization];
- 补充
// 作用:按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界
/* 注意:
一个要实现代理的定位失败方法;
二:不能与startUpdatingLocation同时使用*/
[_locationM requestLocation];
指南针
1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
2. 创建CLLcationManager对象,并设置代理
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
3. 调用CLLcationManager对象的startUpdatingHeading方法进行更新设备朝向
[_locationM startUpdatingHeading];
4. 实现代理方法,获取方向参数,根据方向参数旋转图片
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateHeading:(nonnull CLHeading *)newHeading
{
// 获取当前设备朝向(磁北方向)
CGFloat angle = newHeading.magneticHeading;
// 转换成为弧度
CGFloat radian = angle / 180.0 * M_PI;
// 反向旋转指南针
[UIView animateWithDuration:0.5 animations:^{
self.compassView.transform = CGAffineTransformMakeRotation(-radian);
}];
}
** 注意: 获取用户的设备朝向,不需要用户进行定位授权 **
区域监听
1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
2. 创建CLLcationManager对象,并设置代理,请求授权(iOS8.0之后才需要) * 请求前后台定位授权,并配置KEY *
_locationM = [[CLLocationManager alloc] init];
_locationM.delegate = self;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[_locationM requestAlwaysAuthorization];
}
(img)
3. 调用CLLcationManager对象的startMonitoringForRegion:方法进行监听指定区域
// 创建区域中心
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(29.12345, 131.23456);
// 创建区域(指定区域中心,和区域半径)
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"深圳宝安"];
// 开始监听指定区域
[self.locationM startMonitoringForRegion:region];
4. 实现代理方法,获取区域进入或者离开状态
// 进去监听区域后调用(调用一次)
-(void)locationManager:(nonnull CLLocationManager *)manager didEnterRegion:(nonnull CLRegion *)region
{
NSLog(@"进入区域---%@", region.identifier);
[manager stopMonitoringForRegion:region];
}
// 离开监听区域后调用(调用一次)
-(void)locationManager:(nonnull CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region
{
NSLog(@"离开区域---%@", region.identifier);
}
(反)地理编码
// 1. 导入CoreLocation框架和对应的主头文件
#import <CoreLocation/CoreLocation.h>
// 2. 创建CLGeocoder对象
_geoC = [[CLGeocoder alloc] init];
// 3. 地理编码
// - 方案1:
// 地理编码方案一:直接根据地址进行地理编码(返回结果可能有多个,因为一个地点有重名)
[self.geoC geocodeAddressString:@"人民广场" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
// 包含区,街道等信息的地标对象
CLPlacemark *placemark = [placemarks firstObject];
// 城市名称
// NSString *city = placemark.locality;
// 街道名称
// NSString *street = placemark.thoroughfare;
// 全称
NSString *name = placemark.name;
self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
}];
// - 方案2:
// 地理编码方案二:根据地址和指定区域两个条件进行地理编码(更加精确)
[self.geoC geocodeAddressString:@"广州" inRegion:nil completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
// 包含区,街道等信息的地标对象
CLPlacemark *placemark = [placemarks firstObject];
self.addressDetailTV.text = placemark.description;
self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
}];
// 4. 反地理编码
// 创建CLLocation对象
CLLocation *location = [[CLLocation alloc] initWithLatitude:23.132931 longitude:113.375924;
// 根据CLLocation对象进行反地理编码
[self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
// 包含区,街道等信息的地标对象
CLPlacemark *placemark = [placemarks firstObject];
// 城市名称
// NSString *city = placemark.locality;
// 街道名称
// NSString *street = placemark.thoroughfare;
// 全称
NSString *name = placemark.name;
self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
}];
定位的第三方框架
-
- 下载框架(locationManager框架) [ locationManager框架 下载地址]
-
- 导入框架(直接拖入项目)
-
- 框架功能说明
- 可以使用block进行获取用户位置
- 可以设置超时时长
- 可以取消和强制终止定位
- 框架功能说明
-
- 按照github中,该框架的readme参照使用