小程序判断是否授权位置信息和手动授权

文章讲述了在微信小程序中,如何处理首次打开页面时请求用户位置授权的问题,以及授权成功后的地理编码操作。作者分享了使用wx.getSetting和uni.getLocationAPI获取和处理用户地理位置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概要

当用户来到页面之后就会弹出是否授权弹框,但是如果第一次关闭之后,下一次再过来的话页面的授权弹框就不出现了,针对于这种情况写了一个方法

微信小程序的,使用的是高德地图

	getLocationPodel() {
				let that = this
			
					wx.getSetting({
					    success: (res) => {
					     if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
						  wx.showModal({
					       title: '是否授权当前位置',
					       content: '需要获取您的地理位置,请确认授权,否则无法获取您所需数据',
					       success: function (res) {
					        if (res.cancel) {
					         
					        } else if (res.confirm) {
					         wx.openSetting({
					          success: function (dataAu) {
					           if (dataAu.authSetting["scope.userLocation"] == true) {
					            wx.showToast({
					             title: '授权成功',
					             icon: 'success',
					             duration: 1000
					            })
					            //再次授权,调用getLocationt的API
					            that.getLocation1();
					           } else {
					            wx.showToast({
					             title: '授权失败',
					             icon: 'success',
					             duration: 1000
					            })
					           }
					          }
					         })
					        }
					       }
					      })
					     } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
					      that.getLocation1();
					     }
					     else { //授权后默认加载
					      that.getLocation1();
					     }
					    }
					})
			
			},
			getLocation1() {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					success: function (res) {
						that.getLocation(res.latitude,res.longitude)
					},
					fail:function(res) {
						console.log('dfvdfvdfv')
					}
				});
			},
			getLocation(latitude,longitude) {
				let that = this
				 //根据经纬度获取城市信息
				wx.request({
					url: 'https://apis.map.qq.com/ws/geocoder/v1',
					data: {
						key:'自己的key值,
					    location:`${latitude},${longitude}`
					},
					success: (res) => {
						let address = res.data.result.address_component
						that.city = address.city
						that.province = address.province
					},
					fail: () => {},
					complete: () => {}
				});
			},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值