文档

接入 iOS

更新时间:

本文将介绍如何将定位插件接入到 iOS 客户端。定位 SDK 是一套简单的 LBS (Location-based services) 定位接口,您可以使用这套定位 API 获取定位结果。

SDK 支持 基于 mPaaS 框架接入基于已有工程且使用 mPaaS 插件接入 以及 基于已有工程且使用 CocoaPods 接入 三种接入方式。您可以参考 接入方式介绍,根据实际业务情况选择合适的接入方式。

前置条件

您已经接入工程到 mPaaS。更多信息,请参见以下内容:

添加 SDK

  1. 根据您采用的接入方式,请选择相应的添加方式。

    • 使用 mPaaS Xcode Extension。此方式适用于采用了 基于 mPaaS 框架接入基于已有工程且使用 mPaaS 插件接入 的接入方式。

      1. 点击 Xcode 菜单项 Editor > mPaaS > 编辑工程,打开编辑工程页面。

      2. 选择 移动定位,保存后点击 开始编辑,即可完成添加。image.png

    • 使用 cocoapods-mPaaS 插件。此方式适用于采用了 基于已有工程且使用 CocoaPods 接入 的接入方式。

      1. 在 Podfile 文件中,使用 mPaaS_pod "mPaaS_LBS" 添加移动定位组件依赖。image.png

      2. 在命令行中执行 pod install 即可完成接入。

  2. 打开定位提醒。image.png

使用 SDK

本文将结合 定位 官方 Demo 介绍如何在 10.1.32 及以上版本的基线中使用定位 SDK。

目前,在 APMobileLBS 模块中,提供了获取当前位置的经纬度信息方法。

说明

定位服务目前暂不支持逆地理查询功能,您可调用高德接口进行逆地理查询。

API 说明

参见以下代码,了解定位服务相关接口,通过注释获取接口和相关参数说明。

使用 MPLBSConfiguration 配置参数

/**
 定位服务的配置
 */
@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

使用 MPLBSLocationManager 发起定位请求

/**
 定位结果的回调 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;

回调中 MPLBSLocationInfo 的说明

/**
 逆地理信息
 */
@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 适配

在 iOS 14 中,精确位置作为一个权限选项,在申请定位权限时供用户主动选择,并且在定位权限设置页面可供用户调整。image.pngimage.png

入参适配

在 MPLBSConfiguration 中,增加了 highAccuracyRequired 设置,如果入参 highAccuracyRequired = YES,用户关闭高精度定位权限,则回调错误。

/**
 定位服务的配置
 */
@interface MPLBSConfiguration : NSObject

/**
 *  是否需要高精度定位,iOS 14 以下不区分精度,iOS 14 及以上默认 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];
        });
    }];
}
  • 本页导读 (0)
文档反馈