收藏
回答

使用wx.startLocationUpdateBackground向用户申请获取位置权限发生异常?

使用 使用wx.onLocationChange获取用户当前位置时,配合使用wx.startLocationUpdateBackground向用户申请获取位置权限,在选择组件默认的 选项 ‘在使用小程序期间’ 允许后, 在开发者工具上测试一切正常,但如果在真机测试 下 选择默认选项 ‘在使用小程序期间’ 选项 允许后,结果会异常走 拒绝的fail,但使用当报fail 后使用 wx.openSetting 授权页面 引导用户重新开启权限时则 发现实际上却已经开启了 ‘在使用期间’ 的授权,

所使用的基础版本库2.19.3,在开发者工具测试没问题,在真机调试的时候会出现如上异常走fail 报错的问题,测试的机型有 iphone7Plus,小米6,华为P40,iphone11,iphone12, 尝试过几个版本库都一样,不知道是什么问题照成的,代码片段如下:

代码片段地址:https://developers.weixin.qq.com/s/PZg1Xdmj7Yt3

这个问题困扰我好几天了没有想到解决办法,求大神及官方帮忙看看到底是什么原因导致的,感激不尽,谢谢!


回答关注问题邀请回答
收藏

2 个回答

  • 三十 已过
    三十 已过
    发表于移动端
    2022-05-12
    6五百
    2022-05-12
    有用
    回复
  • H1HAO 🇨🇳
    H1HAO 🇨🇳
    2021-09-10

    权限验证有点问题可以参考我写的权限验证


    // 定位
    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();
    			}
    		})
    	})
    }
    
    2021-09-10
    有用
    回复 3
    • H1HAO 🇨🇳
      H1HAO 🇨🇳
      2021-09-10
      scope.userLocation 注意看这个是否是true,如果是true还走fail就是获取成功后某个步骤语法有错误,可以一步一步打印在哪个地方出了问题
      2021-09-10
      回复
    • Sin²X
      Sin²X
      2021-09-10回复H1HAO 🇨🇳
      一步步测过了,在开发者工具调试没问题,但是到一用真机测试就会走fail那一步,但是实际结果又是成开始了定位权限,想不通
      2021-09-10
      回复
    • H1HAO 🇨🇳
      H1HAO 🇨🇳
      2021-09-10回复Sin²X
      真机测试打印成功和失败,看下提示的内容是什么,根据内容进行排查
      2021-09-10
      回复
登录 后发表内容