index.vue
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<template>
<div class="map-box" :style="{height: mapHeight+ 'px'}">
<div id="container" style="width: 100%; height: 100%"></div>
<div class="map-address">{{ address }}</div>
<div id="myPageTop" style="position: absolute; top: 10px; width: 50%; left: 25%">
<el-input placeholder="请输入关键字" v-model="input" class="input-with-select">
<el-button slot="append" icon="el-icon-search" @click="searchAddress"></el-button>
</el-input>
</div>
</div>
</template>
<script>
import AMap from "AMap";
let map, placeSearch;
export default {
name: "map",
props: {
mapHeight: {
type: Number,
default: 300
}
},
data() {
return {
input: "",
address: "",
};
},
methods: {
mapInit() {
map = new AMap.Map("container", {
resizeEnable: true,
zoom: 10,
});
AMap.service(["AMap.PlaceSearch"], function () {
//构造地点查询类
placeSearch = new AMap.PlaceSearch({
citylimit: false, //是否强制限制在设置的城市内搜索
map: map, // 展现结果的地图实例
autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
renderStyle: "default",
});
});
// 添加列表点选监听事件
AMap.event.addListener(map, "click", this.getAddress);
AMap.event.addListener(placeSearch, "selectChanged", this.getAddress);
},
getAddress(e) {
let self = this;
let lnglat;
if (e.type == "click") {
lnglat = e.lnglat
} else {
console.log(e.selected.data.location)
let { lng, lat } = e.selected.data.location;
lnglat = [lng, lat];
}
AMap.plugin("AMap.Geocoder", () => {
const geocoder = new AMap.Geocoder();
geocoder.getAddress(lnglat, (status, result) => {
self.address = result.regeocode.formattedAddress;
});
});
},
searchAddress() {
placeSearch.search(this.input, function (status, result) {
if (status == "complete") {
// console.log(result.poiList.pois);
}
});
},
selectAddress(e) {
//这里获得点选地点的经纬度
this.getAddress();
},
},
mounted() {
this.mapInit();
}
};
</script>
<style scoped lang="scss">
.map-box {
position: relative;
width: 100%;
}
#container {
position: absolute;
height: 100%;
}
#panel {
position: absolute;
max-height: 90%;
overflow-y: auto;
top: 80px;
right: 10px;
width: 280px;
}
.map-address {
position: absolute;
line-height: 20px;
bottom: -30px;
}
</style>