美文网首页
iOS CoreLocation的使用及子线程定位

iOS CoreLocation的使用及子线程定位

作者: iOS_tree | 来源:发表于2024-01-02 13:31 被阅读0次

当我们需要使用定位功能时,就需要使用到苹果的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]];

}

相关文章

  • CoreLocation使用问题

    iOS8中使用CoreLocation定位

  • iOS CoreLocation 定位实现

    CoreLocation 定位 iOS 地图 反编码 CoreLocation 实现基于 8.0 之后的使用方法,...

  • 关于地图定位

    CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...

  • iOS中的定位功能

    iOS中的定位功能 CoreLocation框架(CoreLocation.framework)可用于定位设备当前...

  • CLLocationManager

    1、定位 使用CoreLocation框架 2、IOS8、IOS9之后的改变 IOS8之后添加的功能 (1)定位服...

  • IOS-GPS定位

    一.介绍 1.定位使用的是: CoreLocation 框架 2. ios8 ios9之后的定位的改变 1⃣️...

  • 学习随记 - 定位服务

    1. IOS定位服务的开启与基本设置 1. 要想使用IOS中的定位服务首先需要包含头文件CoreLocation/...

  • 定位

    定位 1.实现定位功能 在iOS中使用定位功能,需要导入CoreLocation.h文件,其实现定位功能的步骤如下...

  • CoreLocation框架

    CoreLocation框架 一. iOS8.0之前的定位 1. 前台定位 导入CoreLocation框架以及对...

  • iOS位置服务权限相关

    整个iOS系统的定位服务是否开启 #import 当前...

网友评论

      本文标题:iOS CoreLocation的使用及子线程定位

      本文链接:https://www.haomeiwen.com/subject/pdvpndtx.html