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"; import TencentLocationRequest from "com.tencent.map.geolocation.TencentLocationRequest"; export function requestPremission() { // 注册一个请求回调 onAppActivityRequestPermissionsResult((requestCode: number, permissions: MutableList, grantResults: MutableList) => { /** * 0 已同意 * -1 已拒绝 */ console.log(grantResults); console.log(permissions); console.log(requestCode); }); // 发起权限申请 ActivityCompat.requestPermissions( getUniActivity()!, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION), 1001); // 请求权限 return { name: "requestPremission"}; } /** * 定位请求参数封装 */ type LocationOptions = { geocode:boolean, success: (response:LocationResponse) => void; }; /** * 定位返回结果封装 */ type LocationResponse = { name?:string, address?:string, latitude?:number, longitude?:number } class LocationOptionsWapper{ hostOption:LocationOptions; constructor(option: LocationOptions){ this.hostOption = option } onLocationChanged(location:TencentLocation , error:Int , reason:string){ let response = new LocationResponse(); response.name = location.name; response.address = location.address; response.latitude = location.latitude; response.longitude = location.longitude; this.hostOption.success(response); } onStatusUpdate(name:string, status:Int, desc:string){ // 定位状态发生变化 //hostOption.onStatusUpdate(name,status,desc); } }; /** * Tencent 定位监听实现类 */ class SingleLocationListener extends TencentLocationListener { hostOptionWraper:LocationOptionsWapper; constructor(option: LocationOptionsWapper){ super(); this.hostOptionWraper = option } override onLocationChanged(location:TencentLocation , error:Int , reason:string ):void{ console.log(error); console.log(reason); console.log(location); this.hostOptionWraper.onLocationChanged(location,error,reason); } override onStatusUpdate(name:string, status:Int, desc:string ):void{ console.log(name); this.hostOptionWraper.onStatusUpdate(name,status,desc); } } /** * 腾讯地图获取定位信息 * 参考文档:https://lbs.qq.com/mobile/androidLocationSDK/androidGeoGuide/androidGeoAdapt */ export function getLocation(locationOptions: LocationOptions) { let mLocationManager = TencentLocationManager.getInstance(getAppContext()); // 定位监听器封装 let locationOptionWrapper = new LocationOptionsWapper(locationOptions); let mLocationListener = new SingleLocationListener(locationOptionWrapper); // 发起单次请求 let locationRequest = TencentLocationRequest.create() // 是否需要逆地理编码 if(locationOptions.geocode){ locationRequest.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_ADMIN_AREA); }else{ locationRequest.setRequestLevel(TencentLocationRequest.REQUEST_LEVEL_GEO); } console.log(locationRequest); mLocationManager.requestSingleFreshLocation(null, mLocationListener, Looper.getMainLooper()); return { name: "getLocation"}; }