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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| <template> <div> <el-select style="width: 100%; margin-bottom: 10px" v-model="value" filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="handleSearch" :loading="loading" @change="selectAddress" > <el-option v-for="item in suggestionList" :key="item.id" :label="item.title" :value="item.id" > </el-option> </el-select> <div class="map-wrap"> <div id="mapContainer" style="width: 100%; height: 500px"></div> <el-button class="confirm-btn" type="primary" circle @click="confirmMap" >确定</el-button > </div> </div> </template>
<script> import axios from "axios"; export default { data() { return { map: null, search: null, suggest: null, markers: null, suggestionList: [], marker: null, loading: false, value: "", info: null, }; }, created() { const oldAddEventListener = EventTarget.prototype.addEventListener; EventTarget.prototype.addEventListener = function ( key, funcs, options = {} ) { oldAddEventListener.call(this, key, funcs, { passive: false }); }; }, mounted() { this.initMap(); }, methods: { initMap() { const center = new TMap.LatLng(39.916527, 116.397128); this.map = new TMap.Map("mapContainer", { center: center, zoom: 12, }); this.search = new TMap.service.Search({ pageSize: 10 }); this.suggest = new TMap.service.Suggestion({ pageSize: 10, region: "", regionFix: false, }); this.markers = new TMap.MultiMarker({ map: this.map, geometries: [], }); this.info = new TMap.InfoWindow({ map: this.map, position: this.map.getCenter(), }).close(); this.map.on("click", async (evt) => { if (this.marker) this.marker.setMap(null); let poi = evt.poi; if (poi) { this.info.setContent(poi.name).setPosition(poi.latLng).open(); const location = new TMap.LatLng(poi.latLng.lat, poi.latLng.lng); const result = await this.suggest.getSuggestions({ keyword: poi.name, location, }); let currentPoi = result.data[0]; let newPoi = { title: currentPoi.title, address: currentPoi.address, id: currentPoi.id, lat: currentPoi.location.lat, lng: currentPoi.location.lng, }; this.selectSite = newPoi; } else { this.info.close(); } }); },
handleSearch(query) { this.loading = true; this.searchAddress(query); }, async searchAddress(query) { if (query !== "") { this.loading = true; const result = await this.suggest.getSuggestions({ keyword: query, }); this.showSuggestions(result.data); } else { this.options = []; } },
showSuggestions(list) { this.loading = false; this.suggestionList = list.map((item) => ({ title: item.title, address: item.address, id: item.id, lat: item.location.lat, lng: item.location.lng, })); }, selectAddress(val) { const item = this.suggestionList.find((item) => item.id === val); console.log("选中地址:", item); this.selectSite = item; const position = new TMap.LatLng(item.lat, item.lng);
if (this.marker) this.marker.setMap(null); this.marker = new TMap.MultiMarker({ map: this.map, geometries: [{ position }], }); this.info.setContent(item.title).setPosition(position).open();
this.map.setCenter(position); }, confirmMap() { this.$emit("select", this.selectSite); }, }, }; </script>
<style> .map-wrap { position: relative; } .confirm-btn { position: absolute; bottom: 10px; right: 10px; z-index: 1001; padding: 20px 10px !important; font-size: 24px; } </style>
|