index.uts 1.9 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
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
import {
	onAppActivityPause,
    onAppActivityRequestPermissionsResult,
	getUniActivity,
	getAppContext
} from "io.dcloud.uts.android";

import ActivityCompat from "androidx.core.app.ActivityCompat";
import Manifest from "android.Manifest";
import Looper from "android.os.Looper";
import TencentLocationManager from "com.tencent.map.geolocation.TencentLocationManager";
import TencentLocationListener from "com.tencent.map.geolocation.TencentLocationListener";
import TencentLocation from "com.tencent.map.geolocation.TencentLocation";

export function requestPremission() {
	
	// 注册一个请求回调
	// onAppActivityRequestPermissionsResult((requestCode: number,
 //                                                     permissions: Array<string>,
 //                                                     grantResults: Array<number>) => {
	// 	let eventName = "onAppActivityRequestPermissionsResult - " + Date.now();
	//     console.log(eventName);
	// });
	//发起权限申请
	// let permissions:string[] = [Manifest.permission.ACCESS_COARSE_LOCATION]
	ActivityCompat.requestPermissions(
	            getUniActivity()!,
	            arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), 1001);
	
  // 请求权限
  return { name: "requestPremission"};
}


class SingleLocationListener extends TencentLocationListener {
    
	override onLocationChanged(location:TencentLocation , error:Int ,
								  reason:string ):Unit{
		console.log(location);
	}

	override onStatusUpdate(name:string, status:Int, desc:string ):Unit{
		console.log(name);
	}
}

export function getLocation() {
	
  // 获取当前的地址回调
  let mLocationManager = TencentLocationManager.getInstance(getAppContext());
  let mLocationListener = new SingleLocationListener();
  mLocationManager.requestSingleFreshLocation(null, mLocationListener, Looper.getMainLooper());
  // 请求权限
  return { name: "getLocation"};
}