我用的是腾讯地图,来获取逆解析
1.首先要去腾讯地图申请key 腾讯位置服务 - 立足生态,连接未来 进入这个网址
2.
2. 点击 创建应用
// 获取当前城市的定位
getLocation() {
let vm = this;
uni.getLocation({
type: "wgs84",
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
uni.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=你申请的key`,
success: function(res) {
const city = res.data.result.address_component.city;
if (city !== vm.cityName) {
// 如果城市不相等,提示用户是否切换
uni.showModal({
content: `检测到您当前在${city},是否切换到该城市?`,
showCancel: true, // 显示取消按钮
success(res) {
if (res.confirm) {
vm.cityName = city;
uni.setStorageSync('cityName', city);
} else if (res.cancel) {
vm.cityName = '北京'
console.log('取消按钮')
}
}
})
}
}
});
}
});
},
当时做一个问题 ,不知道为啥在小程序里面 不能使用 type:wgs84,但是在app 里面可以使用 在小程序里面用默认为国测局坐标 gcj02