简介
定位,顾名思义,就是确定你所在的位置。
一、介绍
• 定位使用CoreLocation框架
•功能
1、基础定位
2、地理编码反编码
• iOS8 iOS9之后的改变
1、定位服务的目的
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
注意:如果忘记写就不能使用定位功能,没有提示信息
2、请求用户授权
(1)requestAlwaysAuthorization
(2)requestWhenInUseAuthorization
注意:如果和描述的目的不匹配,也不能使用
3、iOS9 按Home键进入后台,如果需要继续定位
(1)需要在info.plist文件里添加Required background modes->App registers for location updates 如果不添加这对键值 却使用后台定位功能 会直接崩溃
(2)allowsBackgroundLocationUpdates 这个属性需要同时设置为YES
二、使用
#import "ViewController.h"
#import<CoreLocation/CoreLocation>
@interface ViewController (){
CLLocationManager *locationManager;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 判断用户是否在设置里面打开了位置服务功能
if (![CLLocationManager locationServicesEnabled]) {
// 1、跳弹出框 提示用户打开步骤
// 2、通过代码跳到设置页面
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请在设置中打开定位功能" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:action];
[self presentViewController:alertController animated:YES completion:nil];
}
// openURL:用于跳转APP 跳到iOS允许跳到的页面
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
// if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
// 跳转到设置页面
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
// }
// [self respondsToSelector:@selector(selector)];//判断方法是否响应
// 1、创建管理者的对象
locationManager = [[CLLocationManager alloc]init];
// 多少米,去更新一次位置信息
locationManager.distanceFilter = 100;
// 设置定位的精准度
locationManager.desiredAccuracy = 10;
// 2、info中添加描述使用定位的目的 并向用户申请授权
[locationManager requestWhenInUseAuthorization];
// 3、 挂上代理 并实现代理方法
locationManager.delegate = self;
// 4、如果需要使用后台定位服务需要在info 中添加Required background modes 这个KEY 以及它里边的元素App registers for location updates
locationManager.allowsBackgroundLocationUpdates = YES;
// 5、开始定位
[locationManager startUpdatingLocation];
}
//定位成功的代理方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"定位成功");
}
//定位失败的代理方法
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"定位失败");
if (error.code == kCLErrorDenied) {
// 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
}
}
如果想要获取定位城市的名字,可在定位成功的代理方法里边添加如下代码:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"定位成功");
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
NSLog(@"locations===%@",[locations lastObject]);
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
{
if (array.count > 0)
{
CLPlacemark *placemark = [array objectAtIndex:0];
NSLog(@"%@",placemark.name);
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"%@",city);
}
else if (error == nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
网友评论