其他分享
首页 > 其他分享> > ios原生实现获取用户位置,用CLLocationManager实现定位

ios原生实现获取用户位置,用CLLocationManager实现定位

作者:互联网

需求:打开APP获取用户所在城市

通过用系统CLLocationManager来实现,步骤如下:

1.在项目的Info.plist中添加两个字段,如下图所示:

2.在AppDelegate中初始化CLLocationManager对象,代码如下:

#import "AppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#import "MainViewController.h"

@interface AppDelegate ()<CLLocationManagerDelegate>

@property(nonatomic,strong)CLLocationManager *locationManager;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //判断是否有定位权限
    if ([CLLocationManager locationServicesEnabled]) {
        // 开启定位
        [self.locationManager startUpdatingLocation];
    }else{
        NSLog(@"系统定位尚未打开,请到【设置-隐私-定位服务】中手动打开");
    }
    
    return YES;
    
}

#pragma mark -定位设置
-(CLLocationManager *)locationManager{
    if (!_locationManager) {
        // 创建CoreLocation管理对象
        CLLocationManager *locationManager = [[CLLocationManager alloc]init];
        // 定位权限检查
        [locationManager requestWhenInUseAuthorization];
        // 设定定位精准度
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        // 设置代理
        locationManager.delegate = self;
        
        _locationManager = locationManager;
    }
    return _locationManager;
    
}

3.实现CLLocationManager相应的代理方法,代码如下

#pragma mark -代理方法,定位权限检查
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:{
            NSLog(@"用户还未决定授权");
            // 主动获得授权
            [self.locationManager requestWhenInUseAuthorization];
            break;
        }
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"访问受限");
            // 主动获得授权
            [self.locationManager requestWhenInUseAuthorization];
            break;
        }
        case kCLAuthorizationStatusDenied:{
            // 此时使用主动获取方法也不能申请定位权限
            // 类方法,判断是否开启定位服务
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"定位服务开启,被拒绝");
            } else {
                NSLog(@"定位服务关闭,不可用");
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways:{
            NSLog(@"获得前后台授权");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse:{
            NSLog(@"获得前台授权");
            break;
        }
        default:
            break;
    }
}
#pragma mark -获取位置
- (void)locationManager:(CLLocationManager *)manager
   didUpdateLocations:(NSArray *)locations{
    
    CLLocation * newLocation = [locations lastObject];
    // 判空处理
    if (newLocation.horizontalAccuracy < 0) {
        NSLog(@"定位失败,请检查手机网络以及定位");
        return;
    }
    //停止定位
    [self.locationManager stopUpdatingLocation];
    // 获取定位经纬度
//    CLLocationCoordinate2D coor2D = newLocation.coordinate;
//    NSLog(@"纬度为:%f, 经度为:%f", coor2D.latitude, coor2D.longitude);
    
    // 创建编码对象,获取所在城市
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    // 反地理编码
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error != nil || placemarks.count == 0) {
            return ;
        }
        // 获取地标
        CLPlacemark *placeMark = [placemarks firstObject];
//        NSLog(@"获取地标 = %@,",placeMark.locality);
    }];
   
}
#pragma mark -定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//     NSLog(@"定位失败,请检查手机网络以及定位");
}

 

 

<style></style>

标签:case,定位,实现,ios,NSLog,break,locationManager,CLLocationManager
来源: https://www.cnblogs.com/zk1947/p/11806337.html