提交 a53f1b66 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

iOS 端 TencentLBS 示例添加注释

上级 b6fc52e2
...@@ -22,6 +22,9 @@ type LocationResponse = { ...@@ -22,6 +22,9 @@ type LocationResponse = {
longitude: number longitude: number
} }
/**
* 定位 LBSLocation 类,封装定位相关方法
*/
class LBSLocation { class LBSLocation {
// 定义 locationManager 属性,类型为 TencentLBSLocationManager // 定义 locationManager 属性,类型为 TencentLBSLocationManager
...@@ -36,6 +39,7 @@ class LBSLocation { ...@@ -36,6 +39,7 @@ class LBSLocation {
// infoDictionary 获取的值类型为 any? // infoDictionary 获取的值类型为 any?
if (apiKey == null) { if (apiKey == null) {
// 如果 apiKey 为 null 返回 false // 如果 apiKey 为 null 返回 false
console.log("apiKey 未配置")
return false return false
} }
// 调用API前需要设置同意用户隐私协议 // 调用API前需要设置同意用户隐私协议
...@@ -44,23 +48,23 @@ class LBSLocation { ...@@ -44,23 +48,23 @@ class LBSLocation {
this.locationManager = new TencentLBSLocationManager() this.locationManager = new TencentLBSLocationManager()
// 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值) // 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值)
this.locationManager.apiKey = apiKey! as string; this.locationManager.apiKey = apiKey! as string;
// this.locationManager.apiKey = "LZTBZ-77PCJ-HJAFN-KWXJ2-H357V-DJBK4"
this.locationManager.allowsBackgroundLocationUpdates = true
} }
return true return true
} }
// 请求定位权限
static requestPremission() { static requestPremission() {
if (this.configLocationManager()) {
this.configLocationManager() const status = CLLocationManager.authorizationStatus()
// 如果未获取过定位权限,则发起权限请求
const status = CLLocationManager.authorizationStatus() if (status == CLAuthorizationStatus.notDetermined) {
if (status == CLAuthorizationStatus.notDetermined) { this.locationManager.requestWhenInUseAuthorization()
this.locationManager.requestWhenInUseAuthorization() }
} }
} }
// 获取位置信息
static getLocation(locationOptions: LocationOptions): boolean { static getLocation(locationOptions: LocationOptions): boolean {
// 初始化 locationManager // 初始化 locationManager
...@@ -69,11 +73,13 @@ class LBSLocation { ...@@ -69,11 +73,13 @@ class LBSLocation {
return false return false
} }
// 是否需要返回逆地理编码
let requestLevel = TencentLBSRequestLevel.geo let requestLevel = TencentLBSRequestLevel.geo
if (locationOptions.geocode) { if (locationOptions.geocode) {
requestLevel = TencentLBSRequestLevel.name requestLevel = TencentLBSRequestLevel.name
} }
// 请求单次定位信息
this.locationManager.requestLocation(with = requestLevel, locationTimeout = 10, completionBlock = (location?: TencentLBSLocation, err?: Error): void => { this.locationManager.requestLocation(with = requestLevel, locationTimeout = 10, completionBlock = (location?: TencentLBSLocation, err?: Error): void => {
if (location != null) { if (location != null) {
let response = new LocationResponse(); let response = new LocationResponse();
...@@ -91,9 +97,16 @@ class LBSLocation { ...@@ -91,9 +97,16 @@ class LBSLocation {
} }
} }
/**
* 导出请求权限方法
*/
export function requestPremission() { export function requestPremission() {
LBSLocation.requestPremission() LBSLocation.requestPremission()
} }
/*
* 导出请求位置信息方法
*/
export function getLocation(locationOptions: LocationOptions): boolean { export function getLocation(locationOptions: LocationOptions): boolean {
return LBSLocation.getLocation(locationOptions) return LBSLocation.getLocation(locationOptions)
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册