iOS-定位

2016-05-25  本文已影响193人  Rick_Liu

使用地图的CLLocationManager完成定位效果.
例子:定位当前所在位置,把所在城市的城市名显示在label上

IMG_0111.PNG Simulator Screen Shot 2016年5月25日 下午11.18.49.png

实现定位效果步骤如下:
1.导入头文件 <CoreLocation/CoreLocation.h>
2.创建位置管理者 CLLocationManager 的对象,并设置代理
3.实现代理对应协议的方法
4.使用系统自带的 CLGeocoder 类,将经度和纬度解析成地名

代码实现如下:

1.导入头文件

#import <CoreLocation/CoreLocation.h>

2.创建位置管理者,和 CLGeocoder 类的对象.这里把他设置成属性

/** 位置管理者*/
@property (nonatomic,strong) CLLocationManager *LManager ;

/**  将经度和纬度解析成地名*/
@property (nonatomic,strong) CLGeocoder *geocoder;

使用懒加载初始化

//懒加载
- (CLLocationManager *)LManager{
    
    if (!_LManager) {
        
        //创建位置管理者
        _LManager = [[CLLocationManager alloc] init];
        _LManager.delegate = self;
    }
    return _LManager;
}

- (CLGeocoder *)geocoder{
    
    if (!_geocoder) {
        
        _geocoder = [[CLGeocoder alloc] init];
    }
    return _geocoder;
}

在这里需要知道,定位在进入iOS8之后,需要修改plist文件,添加两个字段,分别是 NSLocationAlwaysUsageDescription 以及 NSLocationWhenInUseDescription 如下图所示

11.png

由于只有定位需求,所以在viewDidLoad方法里面直接写定位方法.

首先判断用户是否关闭了定位

//判断用户是否关闭了定位
    if (![CLLocationManager locationServicesEnabled]) {
        
        NSLog(@"定位失败");
        return;
    }

版本适配

//做版本适配(ios8以后才有的方法,所以要做适配)
 if ([self.LManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        
        [self.LManager requestWhenInUseAuthorization];
    }

使用位置管理者,开始更新用户位置

//使用位置管理者,开始更新用户位置
    [self.LManager startUpdatingLocation];

 //iOS9.0以后新的请求定位方法,不能与startUpdatingLocation同时使用
   // [self.LManager requestLocation];

让当前类遵从CLLocationManagerDelegate协议,并实现协议里面的一些方法

CLLocationManagerDelegate
定位失败时调用

/**  定位失败时调用*/
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    NSLog(@"定位失败");
}

位置更新后调用

/**  位置更新后调用*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    CLLocation *location = [locations lastObject];

    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {

        for (CLPlacemark *placemark in placemarks) {
            
            for (NSString *key in placemark.addressDictionary) {
                
                NSLog(@"%@ = %@",key,placemark.addressDictionary[key]);
                [self.label performSelectorOnMainThread:@selector(setText:) withObject:placemark.addressDictionary[@"City"] waitUntilDone:YES];
            }
        }
    }];
    
}

定位服务状态改变时调用

/** 定位服务状态改变时调用*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    
    switch (status) {
        case kCLAuthorizationStatusNotDetermined: {
            NSLog(@"用户还未决定授权");
            if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                
                [manager requestAlwaysAuthorization];
            }
            break;
        }
        case kCLAuthorizationStatusRestricted: {
            NSLog(@"访问受限");
            break;
        }
        case kCLAuthorizationStatusDenied: {
            
            if ([CLLocationManager locationServicesEnabled]) {
                NSLog(@"定位服务开启,被拒绝");
            }
            else{
                NSLog(@"定位服务关闭,不可用");
            }
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways: {
            NSLog(@"获得前后台授权");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse: {
            NSLog(@"获得前后台授权");
            break;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读