浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。
连续定位
下载开发文档
简介

iOS系统定位提供了连续定位的能力,百度定位SDK在此基础上做了封装与优化。具体实现连续定位的方法如下:

1. 引入头文件

在调用定位功能的类中引入 BMKLocationComponent.h 这个头文件。

#import <BMKLocationkit/BMKLocationComponent.h>
2. 配置AK

在调用定位时,需要添加AK,需要注意的是请在 SDK 任何类的初始化以及方法调用之前设置正确的 AK。设置AK的方式如下:

[[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"输入AK" authDelegate:self];
3. 设置期望定位精度

由于苹果系统的首次定位结果为粗定位,其可能无法满足需要高精度定位的场景。

百度提供了 kCLLocationAccuracyBest 参数,设置该参数可以获取到精度在10m左右的定位结果,但是相应的需要付出比较长的时间(10s左右),越高的精度需要持续定位时间越长。

推荐使用kCLLocationAccuracyHundredMeters,一次还不错的定位,偏差在百米左右,超时时间设置在2s-3s左右即可。

//初始化实例
_locationManager = [[BMKLocationManager alloc] init];
//设置delegate
_locationManager.delegate = self;
//设置返回位置的坐标系类型
_locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
//设置距离过滤参数
_locationManager.distanceFilter = kCLDistanceFilterNone;
//设置预期精度参数
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置应用位置类型
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
//设置是否自动停止位置更新
_locationManager.pausesLocationUpdatesAutomatically = NO;
//设置是否允许后台定位
_locationManager.allowsBackgroundLocationUpdates = YES;
//设置位置获取超时时间
_locationManager.locationTimeout = 10;
//设置获取地址信息超时时间
_locationManager.reGeocodeTimeout = 10;

更多详细介绍及其余精度阈值说明,请查看参考手册。

4. 开启持续定位

调用BMKLocationManager提供的startUpdatingLocation方法实现。代码如下:

[self.locationManager startUpdatingLocation];

如果需要持续定位返回地址信息(需要联网),请设置如下:

[self.locationManager setLocatingWithReGeocode:YES];
[self.locationManager startUpdatingLocation];
5. 接收位置更新

实现BMKLocationManagerDelegate代理的BMKLocationManager: didUpdateLocation: orError:方法,处理位置更新。代码如下:

- (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager didUpdateLocation:(BMKLocation * _Nullable)location orError:(NSError * _Nullable)error
{
if (error)
{
NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
} if (location) {//得到定位信息,添加annotation
if (location.location) {
NSLog(@"LOC = %@",location.location);
}
if (location.rgcData) {
NSLog(@"rgc = %@",[location.rgcData description]);
}
if (location.rgcData.poiList) {
for (BMKLocationPoi * poi in location.rgcData.poiList) {
NSLog(@"poi = %@, %@, %f, %@, %@", poi.name, poi.addr, poi.relaiability, poi.tags, poi.uid);
}
}
if (location.rgcData.poiRegion) {
NSLog(@"poiregion = %@, %@, %@", location.rgcData.poiRegion.name, location.rgcData.poiRegion.tags, location.rgcData.poiRegion.directionDesc);
}
}
}
6. 停止持续定位

当不再需要定位时,调用BMKLocationManager提供的stopUpdatingLocation方法停止定位。代码如下:

[self.locationManager stopUpdatingLocation];

上一篇

单次定位

下一篇

后台定位

本篇文章对您是否有帮助?