uni-app获取当前位置

原文链接: https://blog.youkuaiyun.com/qq_42778001/article/details/104922059
因为需要做一个获取附近商家的功能。所以需要获取当前的经度和纬度的值,在原文的基础上修改为自己想要的效果
想要的效果:
在这里插入图片描述
弹出弹框,如果允许直接授权,如果不允许,弹出请授权位置的窗口
在这里插入图片描述
点击确认授权,跳到设置授权页面
在这里插入图片描述
如果打开获取地理位置,否则继续弹出请授权位置的弹框

在methods中写方法:

/ 判断是否已经授权
			getSetting() {
				const that = this
				uni.getSetting({
				  success(res) {
				     console.log(res)
						if (res.authSetting['scope.userLocation']) {
							that.getLocation()
						} else {
							that.getAuthorize()
						}
				  }
				})
			},
			//  获取用户的地理位置,
			getLocation() {
				const that = this
				uni.getLocation({
					type: 'gcj02',
					altitude: true,
					success(res) {
						console.log(res)
					}
				})
			},
			// 用户授权
			getAuthorize() {
				const that = this
				uni.authorize({
					scope: 'scope.userLocation',
					success(res) {
						that.getLocation()
					},
					// 授权失败
					fail(err) {
						uni.showModal({
							title: '提示',
							content: '请授权位置获取附近的商家!',
							showCancel: false,
							confirmText: '确认授权',
							success() {
								uni.openSetting({
									success(res) {
										console.log(res)
									},
									fail(err) {
										console.log(err)
									}
								})
							}
						})
					}
				})
			}
		

在onShow中调用

onShow: function() {
	this.getSetting()
},
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值