首页 > App

uniapp授权位置权限(GPS位置)的例子

2023-12-15 浏览: 45
#第一次调用定位接口时,会弹窗请求是否授权//获取定位getLocation(){uni.getLocation({type:'wgs84',success:(res)=>{//授权成功后,调用console.log(res,'当前位置的经纬度')varlatitude=res.latitudevarlongitude=res.longitudevarspeed=res.speedvaraccuracy=res.accuracy;this.getLocal(latitude,longitude)},fail(err){//授权失败后,调用,可以在这儿调用下面的例子再次进行授权console.log(err,'eee')uni.showToast({title:'授权未通过',icon:'none'})}});},/**获取权限*res.authSetting['scope.userLocation']==undefined表示初始化进入该页面res.authSetting['scope.userLocation']==false表示非初始化进入该页面,且未授权res.authSetting['scope.userLocation']==true表示地理位置授权*/uni.getSetting({success:(res)=>{console.log(res,JSON.stringify(res,'getSetting'))if(res.authSetting['scope.userLocation']!=undefined&&res.authSetting['scope.userLocation']!=true){uni.showModal({title:'请求授权当前位置',content:'需要获取您的地理位置,请确认授权',success:function(res){if(res.cancel){uni.showToast({title:'拒绝授权',icon:'none'})}elseif(res.confirm){uni.openSetting({success:function(dataAu){console.log(dataAu,'openSetting')if(dataAu.authSetting["scope.userLocation"]==true){uni.showToast({title:'授权成功',icon:'none'})//再次授权,调用wx.getLocation的API//vm.getLocation();}else{uni.showToast({title:'授权失败',icon:'none',})}}})}}})}elseif(res.authSetting['scope.userLocation']==undefined){//调用wx.getLocation的APIconsole.log('首次授权')this.getLocation();}else{//调用wx.getLocation的APIthis.getLocation();}},complete(){console.log('getSetting')}})},

华为对位置权限要求比较严格,第二次不要循环弹出权限窗口。

标签: uniapp授权位置权限(GPS位置)的例子