Skip to content

微信小程序——用户拒绝获取地理位置信息后怎么再次去获取

微信小程序 | October 20, 2020


文章链接:https://blog.csdn.net/qq_43201350/article/details/109176367

解决方案: wx.openSetting:调起客户端小程序设置界面

wxml文件

<button open-type='openSetting' bindopensetting="openSetting" size="mini">点击打开设置

js文件(调起客户端小程序设置界面)

	openSetting: function(e) {//跳转授权设置之后的回调
	const that = this
		console.log('跳转授权设置之后的回调...')
		const that = this
		if (e.detail.authSetting['scope.userLocation']) { //此处同上同理
		  wx.getLocation({
			type: 'wgs84',
			success(res) {
			  const latitude = res.latitude
			  const longitude = res.longitude
			  const speed = res.speed
			  const accuracy = res.accuracy
			  that.getLocation() // 再次调用获取位置弹框
			}
		  })
		}
	  }, 

js文件(获取位置弹框)

getLocation() {
    const that = this
    // 获取位置信息弹窗
    wx.getLocation({
        type: 'wgs84',
        success(res) {
            const addressUrl =
                'https://apis.map.qq.com/ws/geocoder/v1/?location=' +
                res.latitude +
                ',' +
                res.longitude +
                '&key=QMEBZ-DD2LV-XEZPT-UUB7G-SJCS7-EVBZD'
            wx.request({
                url: addressUrl,
                method: 'GET',
                success: function (res) {
                    const address = res.data.result.ad_info.city
                    that.setData({
                        address: res.data.result.ad_info.city
                    })
                },
            })
        },
    })
}