更新时间:2020-11-10 14:24
定位 SDK 是一套简单的 LBS (Location-based services) 定位接口,您可以使用这套定位 API 获取定位结果。
您已经接入工程到 mPaaS。更多信息,请参见以下内容:
mPaaS_pod "mPaaS_LBS"
添加移动定位组件依赖。pod install
即可完成接入。本文将结合 定位 官方 Demo 介绍如何在 10.1.32 及以上版本的基线中使用 定位 SDK。
目前,在 APMobileLBS 模块中,提供了获取当前位置的经纬度信息方法。
参见以下代码,了解定位服务相关接口,通过注释获取接口和相关参数说明。
/**
定位服务的配置
*/
@interface MPLBSConfiguration : NSObject
/** 单次定位期望精度,单位米,建议结合业务场景传入一个可接受正数,如500,即500m以内的范围 */
@property (nonatomic, assign) CLLocationAccuracy desiredAccuracy;
/** 单次定位接受的缓存时间,从当前时间往前推,多长时间内的缓存是有效的,推荐设置30s以上的缓存时间 */
@property (nonatomic, assign) APCoreLocationCacheAvaliable cacheTimeInterval;
/** 单次定位或逆地理查询的超时时间,单位秒,默认和最小设置为2s */
@property (nonatomic, assign) NSTimeInterval timeOut;
/** 逆地理查询的信息级别,默认APCoreLocationReGeoLevelDistrict */
@property (nonatomic, assign) LBSLocationReGeoLevel reGeoLevel;
/** 逆地理查询的位置信息,在其中指定经纬度坐标 */
@property (nonatomic, strong) CLLocation *reGeoLocation;
/** 逆地理查询位置信息是否为高德坐标系,默认YES(使用 reGeoLocation 参数时生效) */
@property (nonatomic, assign) BOOL reGeoCoordinateConverted;
/** 是否打开签到功能,默认NO(按需设置打开) */
@property (nonatomic, assign) BOOL needCheckIn;
/**
* 是否需要高精度定位,iOS 14 以下不区分精度,iOS 14 及以上默认 NO (低精度),需要业务指定是否需要高精度定位。
*/
@property (nonatomic,assign) BOOL highAccuracyRequired;
@end
/**
定位结果的回调 block
@param success 是否成功
@param locationInfo 位置信息
@param error 定位失败的错误信息
*/
typedef void(^MPLBSLocationCompletionBlock)(BOOL success,
MPLBSLocationInfo *locationInfo,
NSError *error);
/**
定位服务
*/
@interface MPLBSLocationManager : NSObject
/**
初始化
@param configuration 参数配置
@return 实例
*/
- (instancetype)initWithConfiguration:(MPLBSConfiguration *)configuration;
/**
发起单次定位
@param needReGeocode 是否需要逆地理信息。由于定位服务目前暂不支持逆地理查询功能,此处需传入 NO。
@param block 定位结束的回调block
*/
- (void)requestLocationNeedReGeocode:(BOOL)needReGeocode
completionHandler:(MPLBSLocationCompletionBlock)block;
/**
逆地理信息
*/
@interface MPLBSReGeocodeInfo : NSObject
@property (nonatomic, strong) NSString* country; // 国家
@property (nonatomic, strong) NSString* countryCode; // 国家编码
@property (nonatomic, strong) NSString* provience; // 省
@property (nonatomic, strong) NSString* city; // 城市
@property (nonatomic, strong) NSString* district; // 区
@property (nonatomic, strong) NSString* street; // 街道
@property (nonatomic, strong) NSString* streetCode; // 街道编码
@property (nonatomic, strong) NSString* cityCode; // 城市编码
@property (nonatomic, strong) NSString* adCode; // 行政区划编码
@property (nonatomic, strong) NSArray* poiList; // poi 信息列表
@end
/**
定位结果的位置信息数据结构
*/
@interface MPLBSLocationInfo : NSObject
@property (nonatomic, strong) CLLocation* location; // 位置信息
@property (nonatomic, strong) MPLBSReGeocodeInfo* rgcInfo; // 逆地理信息
@end
- (void)getLocation {
MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
configuration.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
[self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
NSString *message;
if (success) {
message = [NSString stringWithFormat:@"定位成功, 经度: %.5f, 维度: %.5f, 精确度: %.3f, 是否高精度 : %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
} else {
message = [NSString stringWithFormat:@"%@", error];
}
dispatch_async(dispatch_get_main_queue(), ^{
AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"定位结果" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
}];
}
在 iOS 14 中,精确位置作为一个权限选项,在申请定位权限时供用户主动选择,并且在定位权限设置页面可供用户调整,如下图所示。
在 MPLBSConfiguration 中,增加了 highAccuracyRequired 设置,如果入参 highAccuracyRequired = YES
,用户关闭高精度定位权限,则回调错误。
/**
定位服务的配置
*/
@interface MPLBSConfiguration : NSObject
/**
* 是否需要高精度定位,iOS14以下不区分精度,iOS14及以上默认NO(低精度),需要业务指定是否需要高精度定位。
*/
@property (nonatomic,assign) BOOL highAccuracyRequired;
@end
//如果入参highAccuracyRequired = YES,且无高精度定位权限则回调错误
Errorcode:APCoreLocationErrorCodeHighAccuracyAuthorization
如果入参 highAccuracyRequired = NO
或者 未设置,则回调的 CLLocation 对象里面会增加字段ap_lbs_is_high_accuracy_close
以标识是否关闭高精度定位。
// 出参改造
@interface CLLocation (APMobileLBS)
/*
* 是否关闭精准定位,默认为NO
*/
@property(nonatomic,assign)BOOL ap_lbs_is_high_accuracy_close;
@end
- (void)getLocationWithHighAccuracy {
MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
configuration.desiredAccuracy = kCLLocationAccuracyBest;
configuration.highAccuracyRequired = YES;
self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
[self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
NSString *message;
if (success) {
message = [NSString stringWithFormat:@"定位成功, 经度: %.5f, 维度: %.5f, 精确度: %.3f, 是否高精度 : %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
} else {
message = [NSString stringWithFormat:@"%@", error];
}
dispatch_async(dispatch_get_main_queue(), ^{
AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"定位结果" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
}];
}
在文档使用中是否遇到以下问题
更多建议
匿名提交