解决uni.getLocation用户端首次拒绝后,点第二次不会再调用的问题

解决方案:给与用户手动指引开启手机定位
效果(测试机为iphone11,机型不一样可能效果会有差别)
1
2
3
4

<template>
	<view class="detail-container">
	   <view class="detail-map" @click="mapClick">
								<text>地图</text>
		</view>
	</view>
	</template>
	<script>
	export default {
		data() {
			return {}
			   },
			   methods: {
			     mapClick() {
			     //目的地的经纬度
			     let item = {
			     latitude:30.67914,
			     longitude:103.960602
			     }
				let that = this;
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						const latitude = Number(item.latitude);
						const longitude = Number(item.longitude);
						console.log("经度", latitude);
						console.log("纬度", longitude);
						uni.openLocation({
							latitude,
							longitude,
							scale: 18
						})
					},fail(res) {
					//用户拒绝后引导用户开启定位
						that.getSetting(item);
					}
				})
			},
			//用户拒绝开启定位后-引导用户手动开启定位
		   // 1.获取设置信息-用户权限列表
		   			getSetting(item) {
		    
		   				uni.getSetting({
		   					success: res => {
		   						console.log('用户权限列表:', res.authSetting)
		   						if (res.authSetting['scope.userLocation']) {
		   							console.log('已授权userLocation')
		   							// 选择位置信息
		   			                this.mapClick(item)// 重新调取uni.getLocation
		   						} else {
		   							console.log('用户未授权userLocation')
		   							//2.用户第一次进来发起授权
		   							uni.showModal({
		   								title: '提示',
		   								content: '当前定位未开启,请点击确定手动开启定位',
		   								duration: 3000,
		   								success: (res) => {
		   									if (res.confirm) {
		        	                           this.openSetting()//点击确定引导客户开启定位
		   									} else if (res.cancel) {
		   										uni.showToast({
		   											title: '你拒绝了授权,无法获取门店定位信息',
		   											duration: 2000,
		   											icon: "none"
		   										});
		   									}
		   								}
		   							});
		   						}
		   					}
		   				})
		   			},
		    
		   			// 4.打开设置
		   			openSetting() {
		   				uni.openSetting({
		   					success: (res) => {
		   						if (res.authSetting['scope.userLocation']) {
		   							// 5.用户在设置中点击了允许,调用选择位置信息函数
		   	                 	     this.mapClick(item)// 重新调取uni.getLocation
		   						} else {
		   							// 5.用户在设置中点击了不允许,展示拒绝授权信息
		   							uni.showToast({
		   								title: '你拒绝了授权,无法操作内容',
		   								icon: "none",
		   								duration: 3000,
		   							})
		   						}
		   					},
		   					fail: (err) => {
		   						console.log("打开设置失败", err)
		   					}
		   				})
		   			},
			   }
			   }
	</script>		   

前端小白在此请各位大佬多多指教鸭!!!!记得给我点个赞哟!!!
查看资料:添加链接描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值