CLLocationManager

CLLocationManager的常用操作和属性

开始用户定位-
(void) startUpdatingLocation
停止用户定位
-(void) stopUpdatingLocation;
说明:当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁 地调用代理的下面方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;  
每隔多少米定位一次
@property(assign, nonatomic) CLLocationDistance distanceFilter;
定位精确度(越精确就越耗电)
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;
只有app在前台运行时,才可以定位
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
无论何时app都可以定位
- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0) __TVOS_PROHIBITED;
临时开启后台定位,退到后台的时候会在设备顶部展示蓝色的定位提示
@property(assign, nonatomic) BOOL allowsBackgroundLocationUpdates __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_9_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED

CLLocationManager常见的代理方法

在定位的成功后不断的调用这个方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_6_0)

过期方法
-- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED


代码

    导入框架 CoreLocation/CoreLocation.h
    #import <CoreLocation/CoreLocation.h>
    
    创建位置管理器(必须自定全局strong,代理方法中可能会用到,局部变)
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    
    请求用户授权 ———> ios8之后才有的  注意:必须配置info.plist文件
    [locationManager requestWhenInUseAuthorization];
    注意:1.requestWhenInUseAuthorization   只有app在前台运行时,才可以定位(常用)
             requestAlwaysAuthorization        无论app在前台还是后台运行,都可以定位
             2.授权只会一次
    
    开始定位
    [locationManager startUpdatingLocation];

info.plist文件配置

配置信息.png

重点说明

设置方法requestWhenInUseAuthorization 或requestAlwaysAuthorization
配置plist文件 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 注意1对1对应

模拟器是不会定位的,需要设置Simulator→Debug→location→Custom Location手动配置地址(首次地址显示的是苹果总部的地址,必须修改下,否则在代理方法打印地址信息可能为空)
模拟器是有bug的,若是发现没法弹出定位弹出框(如代码变化),换个模拟器再试试


代理方法监听位置改变(模拟器需要手动去改变位置)

代理方法一直调用,会非常耗电。除非特殊需求(如导航),可以使用stopUpdatingLocation停止定位,实现一次定位

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //locations是一个CLLocation对象的的泛型数组
    NSLog(@"%@",locations);
    [manager stopUpdatingLocation];
}

单次定位


#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
//需要保证这个属性不被释放,
@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建位置管理器
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        //关联,防止定位弹框被销毁,因为局部变量
    self.locationManager = locationManager;
    
    //2.请求用户授权 ———> ios8之后才有的  注意:必须配置info.plist文件
    [locationManager requestWhenInUseAuthorization];
    
    //3.设置代理
    locationManager.delegate = self;
    //4.开始定位
    [locationManager startUpdatingLocation];
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //locations是一个CLLocation对象的的泛型数组
    NSLog(@"%@",locations);
   // [manager stopUpdatingLocation];
}

@end

多次定位

didUpdateLocations这个代理方法会持续调用
若想实现多次定位
[manager stopUpdatingLocation]; 不写就实现了持续定位功能


临时开启后台定位功能

    在ios9.0以后苹果提供了临时定位的功能
    allowbackgroundLocationUpdates 这个属性可以临时开启后台定位
    但是也必须配置info.plist文件
    (Required background modes 加上这条属性是一个数组,在item0中加一条App registers for updates)

注意:可以通过配置plist文件来加上定位功能,也可以通过项目→Capabilities去选择你想要实现的功能去开启

定位配置.png

如定位就是去选择 background Modes

1.png
这种方式配置后会默认在你的info.plist文件中生成你想要属性

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容