# uniapp小程序中用户拒绝定位之后引导用户开启定位流程(uni-getLocation)

  • 首先判断用户是否打开,若未打开定位信息(或之前已拒绝)加一个系统的弹窗,可以跳到设置页,监听了打开定位之后自动弹出选位置
pickAddress() {
	let that = this
	uni.authorize({
	    scope: 'scope.userLocation',
	    success() {
	        uni.chooseLocation({
	        	success: function (res) {
					console.log(res)
					if(res.name.length == res.address.length &&  res.address.length == 0)
					   return ;
					that.qaddress = res.name || res.address
					that.qlat = res.latitude
					that.qlng = res.longitude
	        	}
	        });
	    },
		fail: error => {
		// console.log("获取定位失败了", error)

		// uni.showToast({

		// 	title: '你拒绝了授权,无法操作内容,请返回上一页',
		// 	icon: "none",
		// 	duration: 3000,
		// })
		this.getSetting()
		}
		
	})
},
getSetting() {
 
	uni.getSetting({
		success: res => {
			console.log('用户权限列表:', res.authSetting)
			if (res.authSetting['scope.userLocation']) {
				console.log('已授权userLocation')
				// 选择位置信息
				this.pickAddress()//-------------------- 重新调取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.pickAddress()//----------------- 重新调取uni.getLocation
			} else {

				// 5.用户在设置中点击了不允许,展示拒绝授权信息
				uni.showToast({

					title: '你拒绝了授权,无法获取地址信息',
					icon: "none",
					duration: 3000,
				})
			}
		},
		fail: (err) => {
			console.log("打开设置失败", err)
		}
	})
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90