index.uts 4.7 KB
Newer Older
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
1
import { CLLocationManager, CLAuthorizationStatus } from "CoreLocation"
2
import { TencentLBSLocationManager, TencentLBSLocation, Error, TencentLBSRequestLevel, TencentLBSLocationManagerDelegate} from "TencentLBS"
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import Bundle from "Foundation"


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

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

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

143 144
const LBSLocationTool: LBSLocation = new LBSLocation()

145
/**
146
 * 请求定位权限方法
147
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
148
export function requestPremission() {
149
	LBSLocationTool.requestPremission()
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
150
}
151 152

/*
153
 * 获取位置信息方法(单次定位)
154
 */
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
155
export function getLocation(locationOptions: LocationOptions): boolean {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	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 已提交
171
}