index.uts 4.6 KB
Newer Older
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
1
import { CLLocationManager, CLAuthorizationStatus } from "CoreLocation"
2 3
import { TencentLBSLocationManager, TencentLBSLocation, TencentLBSRequestLevel, TencentLBSLocationManagerDelegate } from "TencentLBS"
import { NSError, Bundle } from "Foundation"
4
import { LocationOptions, LocationResponse } from "../interface.uts"
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
5

6 7 8 9 10 11 12
/**
 * 判断当前是否是自定义基座
 */
export function checkHasIntegration():boolean{
	// todo
	return true
}
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
13 14


15 16 17
/**
 * 定位 LBSLocation 类,封装定位相关方法
 */
18
class LBSLocation implements TencentLBSLocationManagerDelegate {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
19 20
	
	// 定义 locationManager 属性,类型为 TencentLBSLocationManager
21 22 23
	locationManager: TencentLBSLocationManager
	
	locationOptions?: LocationOptions
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
24 25
	
	// 初始化 locationManager 方法
26
	configLocationManager(): boolean {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
27 28 29 30 31 32 33
		
		if (this.locationManager == null) {
			// 从 info.plist 中读取 apiKey
			const apiKey = Bundle.main.infoDictionary?.["TencentLBSAPIKey"]
			// infoDictionary 获取的值类型为 any?
			if (apiKey == null) { 
				// 如果 apiKey 为 null 返回 false
34
				console.log("apiKey 未配置")
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
35 36 37 38 39 40 41 42
			    return false
			}
			// 调用API前需要设置同意用户隐私协议
			TencentLBSLocationManager.setUserAgreePrivacy(true)
			// 初始化 locationManager 实例对象
			this.locationManager = new TencentLBSLocationManager() 
			// 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值)
			this.locationManager.apiKey = apiKey! as string;
43
			this.locationManager.delegate = this
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
44 45 46 47 48
		}
		
		return true
	}
	
49
	// 请求定位权限
50
	requestPremission() {
51 52 53 54 55 56
		if (this.configLocationManager()) {
			const status = CLLocationManager.authorizationStatus()
			// 如果未获取过定位权限,则发起权限请求
			if (status == CLAuthorizationStatus.notDetermined) {
				this.locationManager.requestWhenInUseAuthorization()
			}
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
57 58 59
		}
	}
	
60 61
	// 获取单次位置信息
	getLocation(locationOptions: LocationOptions): boolean {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
62 63 64 65 66 67 68
		
		// 初始化 locationManager 
		if (!this.configLocationManager()) {
			// 初始化失败返回 false
			return false
		}
		
69
		// 是否需要返回逆地理编码
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
70 71 72 73 74
		let requestLevel = TencentLBSRequestLevel.geo
		if (locationOptions.geocode) {
			requestLevel = TencentLBSRequestLevel.name
		}
		
75
		// 请求单次定位信息
76
		this.locationManager.requestLocation(with = requestLevel, locationTimeout = 10, completionBlock = (location?: TencentLBSLocation, err?: NSError): void => {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
			if (location != null) {
			    let response = new LocationResponse();
			    response.name = location!.name;
			    response.address = location!.address;
			    response.latitude = Number(location!.location.coordinate.latitude);
			    response.longitude = Number(location!.location.coordinate.longitude);
				locationOptions.success(response);
			} else {
				locationOptions.fail(err!.localizedDescription)
			}
		})
		
		return true
	}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	
	// 监听位置变化
	watchPosition(locationOptions: LocationOptions) {
		// 初始化 locationManager
		if (!this.configLocationManager()) {
			return
		}
		if (locationOptions.geocode) {
			this.locationManager.requestLevel = TencentLBSRequestLevel.name
		} else {
			this.locationManager.requestLevel = TencentLBSRequestLevel.geo
		}
		this.locationOptions = locationOptions
		this.locationManager.startUpdatingLocation()
	}
	
	// 清除监听
	clearWatch() {
		// 初始化 locationManager
		if (!this.configLocationManager()) {
			return
		}
		this.locationManager.stopUpdatingLocation()
	}
	
	// 实现定位出错的 delegate 方法
117
	tencentLBSLocationManager(manager: TencentLBSLocationManager, error: NSError) {
118 119 120 121 122 123 124 125 126 127 128 129 130
		this.locationOptions?.fail(error.localizedDescription)
	}
	
	// 实现位置更新的 delegate 方法
	tencentLBSLocationManager(manager: TencentLBSLocationManager, @argumentLabel("didUpdate") location: TencentLBSLocation) {
		let response = new LocationResponse();
		response.name = location.name;
		response.address = location.address;
		response.latitude = Number(location.location.coordinate.latitude);
		response.longitude = Number(location.location.coordinate.longitude);
		this.locationOptions?.success(response)
	}
	
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
131 132
}

133 134
const LBSLocationTool: LBSLocation = new LBSLocation()

135
/**
136
 * 请求定位权限方法
137
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
138
export function requestPremission() {
139
	LBSLocationTool.requestPremission()
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
140
}
141 142

/*
143
 * 获取位置信息方法(单次定位)
144
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
145
export function getLocation(locationOptions: LocationOptions): boolean {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	return LBSLocationTool.getLocation(locationOptions)
}

/**
 * 持续监听位置变化
 */
export function watchPosition(locationOptions: LocationOptions) {
	LBSLocationTool.watchPosition(locationOptions)
}

/**
 * 关闭监听位置变化
 */
export function clearWatch() {
	LBSLocationTool.clearWatch()
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
161
}