iOS CoreLocation的使用及子线程定位

2024-01-02  本文已影响0人  iOS_tree

当我们需要使用定位功能时,就需要使用到苹果的CoreLocation框架。使用步骤为导入头文件、添加info里的定位权限信息、初始化CLLocationManager实例对象、设置定位参数和代理、开始定位、实现代理方法处理数据、使用完成后停止定位。

1.导入框架

#import <CoreLocation/CoreLocation.h>

2.添加info权限请求信息

有几种选择可选,1、请求一次定位权限;2、当app使用时请求定位权限;3、请求总是使用定位权限;
我们一般选择Privacy - Location When In Use Usage Description(当app使用时请求定位权限)即可,如下


配置info文件

3.初始化CLLocationManager实例对象

使用强指针保存实例对象

@property(nonatomic,strong)CLLocationManager* locationManager;

初始化实例对象

self.locationManager = [[CLLocationManager alloc] init];

4.设置定位参数和代理

请求定位权限,设置参数和代理

[self.locationManager requestWhenInUseAuthorization];
self.locationManager.distanceFilter = 2;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;

5.开始定位

[self.locationManager startUpdatingLocation];

6.实现代理方法处理数据

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations API_AVAILABLE(ios(6.0), macos(10.9)) {
    NSLog(@"%@",[NSThread currentThread]);

    NSLog(@"didUpdateLocations %@",locations);
}
- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading API_AVAILABLE(ios(3.0), macos(10.15), watchos(2.0)) API_UNAVAILABLE(tvos) {
    NSLog(@"%@",[NSThread currentThread]);

    NSLog(@"didUpdateHeading %@",newHeading);
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"%@",[NSThread currentThread]);

    NSLog(@"didFailWithError %@",error);
}

7.使用完成后停止定位

[self.locationManager stopUpdatingLocation];

8.在子线程进行定位

CLLocationManager对象的初始化一般在主线程,如果在子线程初始化,则需要开启激活子线程的RunLoop,以此来保持定位数据的接收。官方文档描述如下:

Core Location calls the methods of your delegate object using the NSRunLoop of the thread on which you initialized the CLLocationManager object. That thread must itself have an active NSRunLoop, like the one found in your app’s main thread.

如果需要在子线程进行定位,代码如下:
设置定位和线程对象属性

@property(nonatomic,strong)CLLocationManager* locationManager;

@property(nonatomic,strong)NSThread* locationThread;

创建子线程对象,开启线程

 self.locationThread = [[NSThread alloc] initWithTarget:self selector:@selector(initLocationManager) object:nil];
    
    
[self.locationThread start];

在子线程里初始化CLLocationManager对象,并启动子线程runLoop,添加NSPort使runLoop处于激活状态,接收定位信息

- (void)initLocationManager {
   
    self.locationManager = [[CLLocationManager alloc] init];

    NSLog(@"%@",[NSThread currentThread]);
    [self.locationManager requestWhenInUseAuthorization];
    self.locationManager.distanceFilter = 2;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    [self.locationManager startUpdatingHeading];
    NSPort *port = [[NSPort alloc] init];
    [[NSRunLoop currentRunLoop] addPort:port forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

}
上一篇下一篇

猜你喜欢

热点阅读