uniapp获取用户位置信息

1.高德开放平台创建应用

2.申请key

3.需要用到逆地理编码  所以选择web服务

4.小程序代码中manifest.json文件配置

"permission": {
			"scope.userLocation": {
				"desc": "你的位置信息将用于展示附近服务"
			}
	},
"requiredPrivateInfos": ["getLocation", "chooseLocation"],

5.获取地址代码

// 获取当前位置
			getLocation() {
				uni.getLocation({
					type: 'gcj02',
					success: async (res) => {
						const {
							latitude,
							longitude
						} = res;
						// 调用高德API
						const key = 换成你自己的key; 
						const url =
							`https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}`;
						const response = await uni.request({url});
						console.log('用户所在位置', response[1].data.regeocode.addressComponent.city);
					},
					fail: (err) => {
						uni.showToast({
							title: '获取位置失败',
							icon: 'none'
						});
					}
				});
			},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值