index.uts 4.9 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"
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
4

5 6 7 8 9 10 11
/**
 * 判断当前是否是自定义基座
 */
export function checkHasIntegration():boolean{
	// todo
	return true
}
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

/**
 * 定位请求参数封装
 */
type LocationOptions = {
	geocode: boolean,
	success: (response: LocationResponse) => void,
	fail: (msg: string) => void
};

/**
 * 定位返回结果封装
 */
type LocationResponse = {
	name: string,
	address: string,
	latitude: number,
	longitude: number
}

32 33 34
/**
 * 定位 LBSLocation 类,封装定位相关方法
 */
35
class LBSLocation implements TencentLBSLocationManagerDelegate {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
36 37
	
	// 定义 locationManager 属性,类型为 TencentLBSLocationManager
38 39 40
	locationManager: TencentLBSLocationManager
	
	locationOptions?: LocationOptions
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
41 42
	
	// 初始化 locationManager 方法
43
	configLocationManager(): boolean {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
44 45 46 47 48 49 50
		
		if (this.locationManager == null) {
			// 从 info.plist 中读取 apiKey
			const apiKey = Bundle.main.infoDictionary?.["TencentLBSAPIKey"]
			// infoDictionary 获取的值类型为 any?
			if (apiKey == null) { 
				// 如果 apiKey 为 null 返回 false
51
				console.log("apiKey 未配置")
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
52 53 54 55 56 57 58 59
			    return false
			}
			// 调用API前需要设置同意用户隐私协议
			TencentLBSLocationManager.setUserAgreePrivacy(true)
			// 初始化 locationManager 实例对象
			this.locationManager = new TencentLBSLocationManager() 
			// 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值)
			this.locationManager.apiKey = apiKey! as string;
60
			this.locationManager.delegate = this
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
61 62 63 64 65
		}
		
		return true
	}
	
66
	// 请求定位权限
67
	requestPremission() {
68 69 70 71 72 73
		if (this.configLocationManager()) {
			const status = CLLocationManager.authorizationStatus()
			// 如果未获取过定位权限,则发起权限请求
			if (status == CLAuthorizationStatus.notDetermined) {
				this.locationManager.requestWhenInUseAuthorization()
			}
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
74 75 76
		}
	}
	
77 78
	// 获取单次位置信息
	getLocation(locationOptions: LocationOptions): boolean {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
79 80 81 82 83 84 85
		
		// 初始化 locationManager 
		if (!this.configLocationManager()) {
			// 初始化失败返回 false
			return false
		}
		
86
		// 是否需要返回逆地理编码
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
87 88 89 90 91
		let requestLevel = TencentLBSRequestLevel.geo
		if (locationOptions.geocode) {
			requestLevel = TencentLBSRequestLevel.name
		}
		
92
		// 请求单次定位信息
93
		this.locationManager.requestLocation(with = requestLevel, locationTimeout = 10, completionBlock = (location?: TencentLBSLocation, err?: NSError): void => {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
			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
	}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	
	// 监听位置变化
	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 方法
134
	tencentLBSLocationManager(manager: TencentLBSLocationManager, error: NSError) {
135 136 137 138 139 140 141 142 143 144 145 146 147
		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 已提交
148 149
}

150 151
const LBSLocationTool: LBSLocation = new LBSLocation()

152
/**
153
 * 请求定位权限方法
154
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
155
export function requestPremission() {
156
	LBSLocationTool.requestPremission()
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
157
}
158 159

/*
160
 * 获取位置信息方法(单次定位)
161
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
162
export function getLocation(locationOptions: LocationOptions): boolean {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	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 已提交
178
}