![在这里插入图片描述](https://img-blog.csdnimg.cn/dc4a22bcd008458a914ecc6941ae18eb.png)
坑:1.可能是dialog的层级过高,导致一直无法显示,设置结果类.amap-sug-result的z-index大于1024
2.最坑爹的是代码没问题,而是,输入提示的api每日有次数限制,返回信息也没有返回
<template>
<div>
<table>
<tr>
<td>
<label>请输入具体地址:</label>
</td>
</tr>
<tr>
<td>
<input id="tipinput" />
</td>
</tr>
</table>
<div :style="`width: ${width};height: ${height}`" id="GDmapContainer"></div>
</div>
</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader'
export default {
props: {
width: {
type: [String],
default: '100vw',
},
height: {
type: [String],
default: '100vh',
},
viewMode: {
type: String,
default: '2D',
},
zoom: {
type: Number,
default: 12,
},
center: {
type: Array,
default: () => [120.130396, 30.259242],
},
mapStyle: {
type: String,
default: '',
},
click: {
type: Boolean,
default: false,
},
mousemove: {
type: Boolean,
default: false,
},
dblclick: {
type: Boolean,
default: false,
},
},
data() {
return {
map: null,
marker: null,
AMapCoder: null,
address: '',
markerList: [],
}
},
mounted() {
this.initMap()
},
methods: {
clickHandler(e) {
if (this.marker !== null) {
this.map.remove(this.marker)
}
console.log(e)
const x = e.lnglat.getLng()
const y = e.lnglat.getLat()
console.log(new AMap.LngLat(x, y))
this.marker = new AMap.Marker({
position: new AMap.LngLat(x, y),
offset: new AMap.Pixel(-5, -5),
anchor: 'bottom-center',
})
this.map.add(this.marker)
let address
this.AMapCoder.getAddress(new AMap.LngLat(x, y), (status, result) => {
if (status === 'complete' && result.regeocode) {
address = result.regeocode.formattedAddress
this.$emit('clickHandler', {
x,
y,
address,
})
} else {
console.log('根据经纬度查询地址失败')
}
})
},
mousemoveHandler(e) {
this.$emit('mousemoveHandler', e)
},
dblclickHandler(e) {
this.$emit('dblclickHandler', e)
},
initMap() {
window._AMapSecurityConfig = {
securityJsCode: 'xxx',
}
AMapLoader.load({
key: 'xxx',
plugins: ['AMap.Geocoder', 'AMap.Autocomplete', 'AMap.PlaceSearch'],
})
.then((AMap) => {
this.map = new AMap.Map('GDmapContainer', {
viewMode: this.viewMode,
zoom: this.zoom,
center: this.center,
mapStyle: this.mapStyle || 'amap://styles/darkblue',
})
this.AMapCoder = new AMap.Geocoder({
radius: 1000,
extensions: 'all',
})
const autoOptions = {
input: 'tipinput',
}
const autocomplete = new AMap.Autocomplete(autoOptions)
const xia = document.getElementsByClassName('amap-sug-result')
for (let i = 0; i < xia.length; i++) {
const element = xia[i]
element.style.zIndex = 2500
}
const that = this
AMap.event.addListener(autocomplete, 'select', function (e) {
const lngLat = [e.poi.location.lng, e.poi.location.lat]
const x = lngLat[0]
const y = lngLat[1]
that.map.setCenter(lngLat)
if (that.marker !== null) {
that.map.remove(that.marker)
}
that.marker = new AMap.Marker({
position: new AMap.LngLat(x, y),
offset: new AMap.Pixel(-5, -5),
anchor: 'bottom-center',
})
that.map.add(that.marker)
that.$emit('clickHandler', {
x,
y,
})
})
if (this.click) this.map.on('click', this.clickHandler)
if (this.dblclick) this.map.on('dblclick', this.dblclickHandler)
if (this.mousemove) this.map.on('mousemove', this.mousemoveHandler)
})
.catch((err) => {
throw new Error(err)
})
},
},
}
</script>