uniapp授权位置权限(GPS位置)的例子
# 第一次调用定位接口时,会弹窗请求是否授权 //获取定位 GETLocation() { uni.getLocation({ type: 'wgs84', success: (res) => {//授权成功后,调用 console.log(res,'当前位置的经纬度') var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = 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'}) } else if (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',}) } } }) } } }) } else if (res.authSetting['scope.userLocation'] == undefined) { //调用wx.getLocation的API console.log('首次授权') this.getLocation(); } else { //调用wx.getLocation的API this.getLocation(); } }, complete() { console.log('getSetting') } }) },
华为对位置权限要求比较严格,第二次不要循环弹出权限窗口。
扫描二维码推送至手机访问。
版权声明:本文由网站APP-h5开发APP-跨平台开发工具发布,如需转载请注明出处。