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'
});
}
});
},