index.uts 2.8 KB
Newer Older
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
import { CLLocationManager, CLAuthorizationStatus } from "CoreLocation"
import { TencentLBSLocationManager, TencentLBSLocation, Error, TencentLBSRequestLevel} from "TencentLBS"
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
}

class LBSLocation {
	
	// 定义 locationManager 属性,类型为 TencentLBSLocationManager
	static locationManager: TencentLBSLocationManager
	
	// 初始化 locationManager 方法
	static configLocationManager(): boolean {
		
		if (this.locationManager == null) {
			// 从 info.plist 中读取 apiKey
			const apiKey = Bundle.main.infoDictionary?.["TencentLBSAPIKey"]
			// infoDictionary 获取的值类型为 any?
			if (apiKey == null) { 
				// 如果 apiKey 为 null 返回 false
			    return false
			}
			// 调用API前需要设置同意用户隐私协议
			TencentLBSLocationManager.setUserAgreePrivacy(true)
			// 初始化 locationManager 实例对象
			this.locationManager = new TencentLBSLocationManager() 
			// 设置 apiKey (因为 apiKey 是 any?类型,需要转成 string 类型赋值)
			this.locationManager.apiKey = apiKey! as string;
			// this.locationManager.apiKey = "LZTBZ-77PCJ-HJAFN-KWXJ2-H357V-DJBK4"
			this.locationManager.allowsBackgroundLocationUpdates = true
		}
		
		return true
	}
	
	static requestPremission() {
		
		this.configLocationManager()
		
		const status = CLLocationManager.authorizationStatus()
		if (status == CLAuthorizationStatus.notDetermined) {
			this.locationManager.requestWhenInUseAuthorization()
		}
	}
	
	static getLocation(locationOptions: LocationOptions): boolean {
		
		// 初始化 locationManager 
		if (!this.configLocationManager()) {
			// 初始化失败返回 false
			return false
		}
		
		let requestLevel = TencentLBSRequestLevel.geo
		if (locationOptions.geocode) {
			requestLevel = TencentLBSRequestLevel.name
		}
		
		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
	}
}

export function requestPremission() {
	LBSLocation.requestPremission()
}
export function getLocation(locationOptions: LocationOptions): boolean {
	return LBSLocation.getLocation(locationOptions)
}