使用 使用wx.onLocationChange获取用户当前位置时,配合使用wx.startLocationUpdateBackground向用户申请获取位置权限,在选择组件默认的 选项 ‘在使用小程序期间’ 允许后, 在开发者工具上测试一切正常,但如果在真机测试 下 选择默认选项 ‘在使用小程序期间’ 选项 允许后,结果会异常走 拒绝的fail,但使用当报fail 后使用 wx.openSetting 授权页面 引导用户重新开启权限时则 发现实际上却已经开启了 ‘在使用期间’ 的授权,
所使用的基础版本库2.19.3,在开发者工具测试没问题,在真机调试的时候会出现如上异常走fail 报错的问题,测试的机型有 iphone7Plus,小米6,华为P40,iphone11,iphone12, 尝试过几个版本库都一样,不知道是什么问题照成的,代码片段如下:
代码片段地址:https://developers.weixin.qq.com/s/PZg1Xdmj7Yt3
这个问题困扰我好几天了没有想到解决办法,求大神及官方帮忙看看到底是什么原因导致的,感激不尽,谢谢!
权限验证有点问题可以参考我写的权限验证
// 定位 export function positioning() { return new Promise((resolve, reject) => { var that = this uni.getSetting({ success(res) { if (!res.authSetting['scope.userLocation']) { uni.authorize({ scope: 'scope.userLocation', success() { uni.getLocation({ type: 'wgs84', success: function(data) { resolve(data); // console.log('当前位置的经度:' + data.longitude, '当前位置的纬度:' + data.latitude) }, fail() { uni.showToast({ title: "定位失败!", icon: "error" }) reject(); } }) }, fail() { uni.hideLoading() uni.showModal({ title: '授权提示', content: '获取权限失败,需要获取您的地理位置才能为您提供更好的服务!是否授权获取地理位置?', confirmText: "前往设置", success: function(res) { if (res.confirm) { uni.openSetting({ success() { uni.authorize({ scope: 'scope.userLocation', success() { uni.getLocation({ type: 'wgs84', success: function( data ) { resolve ( data ); // console.log('当前位置的经度:' + data.longitude, '当前位置的纬度:' + data.latitude) }, fail() { uni.showToast({ title: "定位失败!", icon: "error" }) reject (); } }) }, fail() { reject(); } }) } }) } else if (res.cancel) { uni.showToast({ title: "用户授权失败!", icon: "error" }) reject(); } } }); } }) } else { uni.getLocation({ type: 'wgs84', success: function(data) { resolve(data); // console.log('当前位置的经度:' + data.longitude, '当前位置的纬度:' + data.latitude) }, fail(e) { uni.showToast({ title: "定位失败!", icon: "error" }) reject(); } }) } }, fail(err) { uni.showToast({ title: "定位错误", icon: "error" }) reject(); } }) }) }