index.uts 5.0 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
	locationManager!: TencentLBSLocationManager
22 23
	
	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
			if (location != null) {
78 79 80 81 82 83 84 85 86 87 88 89
				
				// 判断 name、address 是否有值
				var name = ""
				var address = ""
				if (location!.name != null) {
					name = location!.name!
				}
				
				if (location!.address != null) {
					address = location!.address!
				}
				
90
				let response: LocationResponse = {
91 92
					name: name,
					address: address,
93 94 95
					latitude: Number(location!.location.coordinate.latitude),
					longitude: Number(location!.location.coordinate.longitude)
				}
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
96 97 98 99 100 101 102 103
				locationOptions.success(response);
			} else {
				locationOptions.fail(err!.localizedDescription)
			}
		})
		
		return true
	}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	
	// 监听位置变化
	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 方法
130
	tencentLBSLocationManager(manager: TencentLBSLocationManager, @argumentLabel("didFailWithError") error: NSError) {
131 132 133 134 135
		this.locationOptions?.fail(error.localizedDescription)
	}
	
	// 实现位置更新的 delegate 方法
	tencentLBSLocationManager(manager: TencentLBSLocationManager, @argumentLabel("didUpdate") location: TencentLBSLocation) {
136 137 138 139 140 141 142 143 144 145 146 147
		
		// 判断 name、address 是否有值
		var name = ""
		var address = ""
		if (location.name != null) {
			name = location.name!
		}
		
		if (location.address != null) {
			address = location.address!
		}
		
148
		let response: LocationResponse = {
149 150
			name: name,
			address: address,
151 152 153
			latitude: Number(location.location.coordinate.latitude),
			longitude: Number(location.location.coordinate.longitude)
		}
154 155 156
		this.locationOptions?.success(response)
	}
	
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
157 158
}

159 160
const LBSLocationTool: LBSLocation = new LBSLocation()

161
/**
162
 * 请求定位权限方法
163
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
164
export function requestPremission() {
165
	LBSLocationTool.requestPremission()
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
166
}
167 168

/*
169
 * 获取位置信息方法(单次定位)
170
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
171
export function getLocation(locationOptions: LocationOptions): boolean {
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	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 已提交
187
}