<template>
<div class="container">
<div id="map-container">地图</div>
</div>
</template>
<script>
export default {
name: '',
components: {},
data () {
return {
map: null,
markers: [],
markersPosition: [],
geoCoder: null
}
},
mounted () {
this.mapInit()
},
methods: {
mapInit () {
this.map = new AMap.Map('map-container', {
zoom: 11,
resizeEnable: true
})
this.geoCoder = new AMap.Geocoder()
this.handlerMapClick()
},
handlerMapClick () {
this.map.on('click', (e) => {
this.markersPosition = [e.lnglat.lng, e.lnglat.lat]
this.removeMarker()
this.setMapMarker()
this.geoCoder.getAddress(this.markersPosition, (status, result) => {
if (status === 'complete' && result.regeocode) {
this.address = result.regeocode.formattedAddress
console.log('点击位置信息:', result.regeocode.formattedAddress)
let adcode = result.regeocode.addressComponent.adcode
let provinceId = parseInt(adcode.substr(0, 2) + '0000')
let cityId = parseInt(adcode.substr(0, 4) + '00')
let areaId = adcode
console.log('点击位置的省市区id:', provinceId, cityId, areaId)
}
})
})
},
setMapMarker () {
let marker = new AMap.Marker({
map: this.map,
position: this.markersPosition
})
this.markers.push(marker)
},
addMarker () {
let lng = Math.random() * 135.05 + 73.3
let lat = Math.random() * 53.33 + 3.51
console.log('添加标记', [lng, lat])
this.map.setFitView()
let marker = new AMap.Marker({
map: this.map,
position: [lng, lat]
})
this.markers.push(marker)
this.map.setFitView()`在这里插入代码片`
AMap.event.addListener(marker, 'click', function (e) {
console.log('点击marker', e)
})
},
removeMarker () {
if (this.markers) {
this.map.remove(this.markers)
}
}
}
}
</script>
<style scoped lang="scss">
#map-container {
width: 70vw;
height: 70vh;
}
</style>
|