<template>
<div
class="amap-page-container"
style="height: 400px; margin-bottom: 200px; background: #ccc"
>
<el-amap-search-box
class="search-box"
:search-option="searchOption"
:on-search-result="onSearchResult"
></el-amap-search-box>
<el-amap
vid="amapDemo"
:center="center"
:zoom="zoom"
class="amap-demo"
:plugin="plugin"
:events="events"
>
<el-amap-marker
v-for="(marker, index) in markers"
:position="marker"
:key="'marker' + index"
></el-amap-marker>
</el-amap>
<div class="toolbar" v-if="loaded">
position: [{{ lng }}, {{ lat }}] address: {{ address }}
</div>
<div v-else>正在定位</div>
</div>
</template>
<style scoped>
.search-box {
position: absolute;
top: 25px;
left: 20px;
}
.amap-page-container {
position: relative;
}
</style>
<script>
module.exports = {
data: function () {
let self = this
return {
markers: [],
searchOption: {
city: '杭州',
citylimit: false
},
zoom: 12,
loaded: false,
center: [121.59996, 31.197646],
plugin: [
{
pName: 'Geolocation',
showCircle: false, //定位成功后用圆圈表示定位精度范围,默认:true
events: {
init(o) {
// o 是高德地图定位插件实例
o.getCurrentPosition((status, result) => {
if (result && result.position) {
self.lng = result.position.lng
self.lat = result.position.lat
self.center = [self.lng, self.lat]
self.address = result.formattedAddress
self.loaded = true
self.$nextTick()
}
})
}
}
}
],
address: '',
events: {
click(e) {
let { lng, lat } = e.lnglat
self.lng = lng
self.lat = lat
// 这里通过高德 SDK 完成。
var geocoder = new AMap.Geocoder({
radius: 1000,
extensions: 'all'
})
geocoder.getAddress([lng, lat], function (status, result) {
if (status === 'complete' && result.info === 'OK') {
if (result && result.regeocode) {
self.address = result.regeocode.formattedAddress
self.$nextTick()
}
}
})
}
},
lng: 0,
lat: 0
}
},
methods: {
addMarker: function () {
let lng = 121.5 + Math.round(Math.random() * 1000) / 10000
let lat = 31.197646 + Math.round(Math.random() * 500) / 10000
this.markers.push([lng, lat])
},
onSearchResult(pois) {
this.markers = []
let latSum = 0
let lngSum = 0
if (pois.length > 0) {
pois.forEach(poi => {
let { lng, lat } = poi
lngSum += lng
latSum += lat
this.markers.push([poi.lng, poi.lat])
})
let mapcenter = {
lng: lngSum / pois.length,
lat: latSum / pois.length
}
this.center = [mapcenter.lng, mapcenter.lat]
}
}
},
watch: {
address: {
handler: function (val, oldVal) {
this.address = val
this.$emit('sendAddress', this.address)
},
// 深度观察监听
deep: true
}
}
}
</script>
amap地图
最新推荐文章于 2024-07-23 11:05:47 发布