- 准备工作,可以先看官方的介绍,JSAPI结合Vue使用,这个不需要在main.js中引入
- index.html中
<script type="text/javascript">
window._AMapSecurityConfig = {
securityJsCode: 'XXX',
}
</script>
- index.vue的html部分
//我是封装在antd的弹窗组件中
<template>
<a-modal title="选择区域" :visible="visible" @ok="handleOk" @cancel="handleCancel" okText="提交" cancelText="取消">
<div id="container">div>
<div class="toolbar">
当前坐标: {{ point[0] }}, {{ point[1] }}
<br />
地址: {{ address }}
div>
a-modal>
template>
- index.vue的script部分
<script>
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
name: 'MapDialog',
data() {
return {
visible: false,
center: [115.97539, 28.715532],
point: [],
map: null,
marker: null,
geocoder: null,
address: null
}
},
methods: {
open({ point, address }) {
if (point && address) {
this.address = address
this.point = point
this.center = point
}
this.visible = true
this.initMap()
},
initMap() {
AMapLoader.load({
key: 'XXX',
version: '2.0',
plugins: [
'AMap.Geocoder',
'AMap.Marker'
]
})
.then(AMap => {
this.map = new AMap.Map('container', {
zoom: 12,
center: this.center
})
if (this.point.length > 0) {
this.addMarker()
}
this.map.on('click', e => {
let lng = e.lnglat.lng
let lat = e.lnglat.lat
this.point = [lng, lat]
this.addMarker()
this.getAddress()
})
})
.catch(e => {
console.log(e)
})
},
addMarker() {
if (this.marker) {
this.marker.setMap(null)
this.marker = null
}
this.marker = new AMap.Marker({
icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
position: this.point,
offset: new AMap.Pixel(-13, -30)
})
this.marker.setMap(this.map)
},
getAddress() {
const self = this
this.geocoder = new AMap.Geocoder({
city: '全国',
radius: 1000,
extensions: 'all'
})
this.geocoder.getAddress(this.point, function(status, result) {
if (status === 'complete' && result.info === 'OK') {
if (result && result.regeocode) {
self.address = result.regeocode.formattedAddress
}
}
})
},
handleOk() {
this.$emit('callback', { point: this.point, address: this.address })
this.map = null
this.marker = null
this.visible = false
},
handleCancel() {
this.map = null
this.marker = null
this.visible = false
}
}
}
</script>
- index.vue的css部分
- 页面效果

逆解析经纬度得到的详细地址
