uniapp 调用高德地图 点击解析地址
<map style="width: 100%; height: 300px;" :latitude="form.lat" :longitude="form.lng" :markers="covers" id="popMap"></map>
data() {
return {
form: {
address: '',
lat: null,
lng: null,
},
covers: []
}
},
onLoad(options) {
this.getlocation()
},
mounted() {
this.addMapEvent()
},
methods: {
addMapEvent() {
let that = this
let maps = uni.createMapContext('popMap', this).$getAppMap()
maps.onclick = function(point) {
that.amapf(point.longitude, point.latitude)
}
},
amapf(longitude, latitude) {
let point = new plus.maps.Point(longitude, latitude);
//静态方法,反向地理编码
plus.maps.Map.reverseGeocode(point, {}, (event) => {
this.form.lat = event.coord.latitude
this.form.lng = event.coord.longitude
this.covers = []
this.covers = [{
id: 0,
longitude: event.coord.longitude,
latitude: event.coord.latitude,
iconPath: '@/static/s.png',
width: 24,
height: 24
}]
let address = event.address; // 转换后的地理位置
this.form.address = address // 重新赋值
}, function(e) {
console.log("失败回调", e);
});
},
getlocation() {
let that = this
uni.getLocation({
type: 'gcj02',
geocode: true,
success(res) {
let point = new plus.maps.Point(res.longitude, res.latitude);
//静态方法,反向地理编码
plus.maps.Map.reverseGeocode(point, {}, (event) => {
let address = event.address; // 转换后的地理位置
that.form.address = address // 重新赋值
}, function(e) {
console.log("失败回调", e);
});
// 赋值经纬度,从而得到当前位置
that.form.lat = res.latitude
that.form.lng = res.longitude
// 地图上显示所在图标开始
let arr = [{
id: 0,
longitude: res.longitude,
latitude: res.latitude,
iconPath: '@/static/s.png',
width: 24,
height: 24
}]
that.covers = arr
}
})
}
}