常见app需求:获取当前用户的经纬度或地址信息,或者通过经纬度计算与某个位置的距离
1.导入coreLocation库
2.在info.plist里设置权限
(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):NSLocationWhenInUseUsageDescription:允许在前台使用时获取GPS的描述NSLocationAlwaysUsageDescription:允许永久使用GPS的描述
3.导入头文件和设置代理
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
/** 定位 */
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
4.实现定位
//调用方法
- (void)viewDidLoad {
[super viewDidLoad];
[self startLocation];
}
//定位
- (void)startLocation {
//判断定位是否允许
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 10.0f;
[_locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
} else {
//如果没有授权定位,提示开启
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"允许定位" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertVC addAction:ensure];
[alertVC addAction:cancel];
[self.navigationController presentViewController:alertVC animated:YES completion:nil];
}
}
#pragma mark - CLLocationManagerDelegate
//更新用户位置
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
YMLog(@"%@",locations);
//当前所在城市的坐标值
CLLocation *currLocation = [locations lastObject];
YMLog(@"当前经度=%f 当前纬度=%f 当前高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
[AppSingleInstance sharedInstance].latitude = currLocation.coordinate.latitude;
[AppSingleInstance sharedInstance].longitude = currLocation.coordinate.longitude;
//根据经纬度反编译地址信息
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
YMLog(@"当前用户所在城市:%@",placeMark.locality);
YMLog(@"%@",placeMark.country);//当前国家
YMLog(@"%@",placeMark.locality);//当前城市
YMLog(@"%@",placeMark.subLocality);//当前位置
YMLog(@"%@",placeMark.thoroughfare)//当前街道
YMLog(@"%@",placeMark.name);//具体地址 市 区 街道
NSString *address = [NSString stringWithFormat:@"%@%@%@",placeMark.locality,placeMark.subLocality,placeMark.name];
YMLog(@"%@",address);
[AppSingleInstance sharedInstance].currentUserAddress = address;
} else if (error == nil && placemarks.count == 0) {
YMLog(@"没有地址返回");
} else if (error) {
YMLog(@"%@",error);
}
}];
}
//定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if (error.code == kCLErrorDenied) {
//访问被拒绝
YMLog(@"位置访问被拒绝");
} else if (error.code == kCLErrorLocationUnknown) {
YMLog(@"无法获取用户信息");
}
}