1.uniapp中manifest.json内web配置(定位和地图)中选择高德地图需要现在高德地图申请
2.将key和密钥填写到web配置中,
3.另申请一个web服务的key,后期请求逆编码需要用到这个key,必须使用web服务的key 拼接使用,
4.将定位获取到的经纬度赋值,url中规则是经度》纬度》web服务的key的顺序写反则返回为空
<script>
export default {
data() {
latitude: 0, //纬度
longitude: 0, //经度
},
methods: {
chooseLocation() {//选择位置
let _this = this
let latitude = Number(_this.latitude);
let longitude = Number(_this.longitude);
uni.chooseLocation({
latitude: latitude,
longitude: longitude,
success: function(res){
console.log('选择位置成功',res)
_this.latitude = res.latitude;
_this.longitude = res.longitude;
//将经纬度参数赋值
let latitude = res.latitude;
let longitude = res.longitude;
let key = '此处为你申请的web服务的key';
_this.loadCity(latitude,longitude,key);
},
fail(res) {
console.log('选择位置失败',res)
}
})
},
loadCity(longitude, latitude,key) {//url中规则是经度在前,纬度在后
uni.request({
header: {
'Content-Type': 'application/text',
},
//注意:这里的key值需要高德地图的 web服务生成的key 只有web服务才有逆地理编码
url: `https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=${latitude},${longitude}&key=${key}&radius=1000&extensions=all`,
success(res) {
console.log(res,"获取位置成功信息")
},
fail(err) {
console.log(err,"获取位置失败信息")
}
})
},
}
}
</script>