解决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>		   

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

猜你喜欢

转载自blog.csdn.net/oneya1/article/details/126440820