diff --git a/pages/SDKIntegration/SDKIntegration.vue b/pages/SDKIntegration/SDKIntegration.vue index 9ae4b9884bc012f95b52b87cb27ad371c0f89216..39a909d0d31f1c9560878fbcf19bd8a3db25fa01 100644 --- a/pages/SDKIntegration/SDKIntegration.vue +++ b/pages/SDKIntegration/SDKIntegration.vue @@ -2,7 +2,8 @@ - + + @@ -13,16 +14,24 @@ export default { data() { return { - title: 'SDK集成示例-开发中', + title: 'SDK集成示例', } }, methods: { - testGetlocation:function(e){ - + + checkLocationPermission:function(e){ requestPremission(); - var locationRet = getLocation(function(res){ - console.log(res); - }); + }, + testGetlocation:function(e){ + getLocation({ + onLocationChanged:function(locationName,locationAddress){ + var addressDesc = locationAddress + '-' + locationName + uni.showToast({ + title:'执行结果:' + addressDesc, + icon:'none' + }); + } + }) }, } diff --git a/uni_modules/uts-tencentgeolocation/utssdk/app-android/index.uts b/uni_modules/uts-tencentgeolocation/utssdk/app-android/index.uts index cdf856b1a9610d71270deab2db4c8f5e657645d4..637d9d8fc5dcffd919c3655a8aa4a467fc904045 100644 --- a/uni_modules/uts-tencentgeolocation/utssdk/app-android/index.uts +++ b/uni_modules/uts-tencentgeolocation/utssdk/app-android/index.uts @@ -15,57 +15,83 @@ import TencentLocation from "com.tencent.map.geolocation.TencentLocation"; export function requestPremission() { // 注册一个请求回调 - // onAppActivityRequestPermissionsResult((requestCode: number, - // permissions: Array, - // grantResults: Array) => { - // let eventName = "onAppActivityRequestPermissionsResult - " + Date.now(); - // console.log(eventName); - // }); - //发起权限申请 - // let permissions:string[] = [Manifest.permission.ACCESS_COARSE_LOCATION] + onAppActivityRequestPermissionsResult((requestCode: number, + permissions: MutableList, + grantResults: MutableList) => { + /** + * 0 已同意 + * -1 已拒绝 + */ + console.log(grantResults); + console.log(permissions); + console.log(requestCode); + }); + // 发起权限申请 + // 参考文档:https://lbs.qq.com/mobile/androidLocationSDK/androidGeoGuide/androidGeoAdapt ActivityCompat.requestPermissions( getUniActivity()!, - arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION), 1001); + arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION), 1001); // 请求权限 return { name: "requestPremission"}; } + +type LocationOptions = { + onLocationChanged: (locationName:string,locationAddress:string ) => void; +}; + +class LocationOptionsWapper{ + + hostOption:LocationOptions; + + constructor(option: LocationOptions){ + this.hostOption = option + } + + onLocationChanged(location:TencentLocation , error:Int , + reason:string){ + + hostOption.onLocationChanged(location.name,location.address); + } + + + onStatusUpdate(name:string, status:Int, desc:string){ + //hostOption.onStatusUpdate(name,status,desc); + } +}; + + class SingleLocationListener extends TencentLocationListener { - constructor(changeListener: (res: string) => void){ + hostOptionWraper:LocationOptionsWapper; + + constructor(option: LocationOptionsWapper){ super(); + this.hostOptionWraper = option } override onLocationChanged(location:TencentLocation , error:Int , reason:string ):void{ - if(error == 0){ - let locationName = location.name; - let locationAddress = location.address; - let latitude = location.latitude; - let longitude = location.longitude; - } - console.log(location); - console.log(error); - console.log(reason); + this.hostOptionWraper.onLocationChanged(location,error,reason); } override onStatusUpdate(name:string, status:Int, desc:string ):void{ - console.log(name); + this.hostOptionWraper.onStatusUpdate(name,status,desc); } + } -export function getLocation(changeListener: (res: string) => void) { +export function getLocation(changeListener: LocationOptions) { // 获取当前的地址回调 let mLocationManager = TencentLocationManager.getInstance(getAppContext()); - - let mLocationListener = new SingleLocationListener( function(newRes:string){ - changeListener(newRes); - }); + let locationOptionWrapper = new LocationOptionsWapper(changeListener); + let mLocationListener = new SingleLocationListener(locationOptionWrapper); + mLocationManager.requestSingleFreshLocation(null, mLocationListener, Looper.getMainLooper()); // 请求权限 return { name: "getLocation"};