jiangping
2024-11-25 4e86fd38a29427a8bb50d73d8eb22f21dfb943d4
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!--
  描述:拖放地图组件,默认尺寸是 500 * 300
 
  接收属性参数:
    lat: 纬度
    lng: 经度
 
  自定义事件:
    drag: 拖放完成事件
 
  示例:
    <mapDrag @drag="dragMap" lat="22.574405" lng="114.095388"></mapDrag>
-->
<template>
  <div class="m-map">
    <div class="search">
      <el-form @submit.native.prevent>
        <input
          :id="search_id"
          v-model="input"
          type="text"
          placeholder="请输入关键字"
          autocomplete="off"
        >
        <input type="text" style="display: none">
        <button type="primary" @click="searchMap">搜索</button>
      </el-form>
      <div v-show="searchKey" id="js-result" class="result" />
    </div>
    <!-- <div id="panel" /> -->
    <!-- 搜索结果栏 -->
    <div v-if="showsearchResult" class="tool_search_result">
      <ul>
        <li
          v-for="(item, index) in poiList"
          :key="index"
          @click="markerResult(item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div id="js-container" class="map">正在加载数据 ...</div>
  </div>
</template>
 
<script>
// import remoteLoad from '@/utils/remoteLoad.js'
import bus from '@/eventBus/eventBus.js'
// import { MapKey, securityJsCode } from '@/api/config'
import AMapLoader from '@amap/amap-jsapi-loader'
const MapKey = 'c477e250c63cde947d7e9072bdcf9e65'
const securityJsCode = '746ae93f3f20c7fc30134109bd55fd19'
export default {
  props: {
    lat: {
      type: String,
      default: ''
    },
    lng: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      autoOptions: {
        input: ''
      },
      showsearchResult: false,
      search_id: 'searchId',
      input: '',
      searchPlaceInput: '',
      searchKey: '',
      placeSearch: null,
      dragStatus: false,
      AMapUI: null,
      AMap: null,
      positionImage: require('@/assets/icons/position.png'),
      marker: {},
      map: null,
      poiList: []
    }
  },
  watch: {
    searchPlaceInput(newValue) {
      if (newValue != null) {
        // this.placeSearch.search(newValue)
      }
    }
  },
  created() {
    bus.$on('share_id', val => {
      this.autoOptions.input = val
    })
    bus.$on('share', val => {
      this.searchPlaceInput = val
    })
  },
  mounted() {
    this.initMap()
    this.sendId()
  },
  methods: {
    sendMsg() {
      bus.$emit('share', this.input)
    },
    sendId() {
      bus.$emit('share_id', this.search_id)
    },
    // 实例化地图
    initMap() {
      window._AMapSecurityConfig = { securityJsCode }
      AMapLoader.load({
        key: MapKey, // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.ToolBar', 'AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.MapType', 'AMap.HawkEye', 'AMap.Scale', 'AMap.AutoComplete'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        this.map = new AMap.Map('js-container', { // 设置地图容器id
          viewMode: '3D', // 是否为3D地图模式
          zoom: 11, // 初始化地图级别
          center: [117.279952, 31.850109] // 初始化地图中心点位置
        })
        this.map.addControl(new AMap.ToolBar())
        this.mapOn(AMap)
      }).catch(e => {
        console.log(e)
      })
    },
    searchMap() {
      var placeSearch = new AMap.PlaceSearch({
        // 构造地点查询类
        pageSize: 30, // 单页显示结果条数
        pageIndex: 1, // 页码
        citylimit: false, // 是否强制限制在设置的城市内搜索
        map: this.map, // 展现结果的地图实例
        // panel: 'panel', // 结果列表将在此容器中进行展示。
        autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
      })
      // 关键字查询
      const cur = this
      placeSearch.search(this.input, (status, result) => {
        // 查询成功时,result即对应匹配的POI信息
        console.log(status)
        console.log(result)
        if (status === 'complete' && result.info === 'OK') {
          cur.showsearchResult = true
          cur.poiList = result.poiList.pois
        } else {
          cur.showsearchResult = false
          cur.poiList = []
          cur.$message({
            message: '没有查到结果',
            type: 'warning'
          })
        }
      })
    },
    markerResult(data) {
      // this.input = data.name
      // this.searchMap()
      const that = this
      const { lat, lng } = data.location
      window._AMapSecurityConfig = { securityJsCode }
      AMapLoader.load({
        key: MapKey, // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.ToolBar', 'AMap.Geocoder', 'AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.MapType', 'AMap.HawkEye', 'AMap.Scale', 'AMap.AutoComplete'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        this.map = new AMap.Map('js-container', { // 设置地图容器id
          viewMode: '3D', // 是否为3D地图模式
          zoom: 18, // 初始化地图级别
          center: [lng, lat] // 初始化地图中心点位置
        })
        this.map.addControl(new AMap.ToolBar())
        const address = data.name
        const marker = new AMap.Marker({
          position: new AMap.LngLat(lng, lat),
          icon: that.positionImage,
          anchor: 'bottom-center'
        })
        // map.remove(that.marker)
        this.map.add(marker)
        that.marker = marker
        that.showsearchResult = false
        that.$emit('center', { lnglat: lng + ',' + lat, address })
        this.mapOn(AMap)
      }).catch(e => {
        console.log(e)
      })
      // var marker = new AMap.Marker({
      //   position: [Number(data.location.lng), Number(data.location.lat)],
      //   cursor: 'pointer',
      //   // icon: carIcon,
      //   autoRotation: true,
      //   angle: 0,
      //   offset: new AMap.Pixel(-36, -30),
      // })
      // marker.setMap(this.mapall)
      // this.mapall.setFitView();
    },
    // 地图事件监听
    mapOn(AMap) {
      const { map } = this
      const that = this
      const geocoder = new AMap.Geocoder()
      let address = ''
      // 监听地图点击事件
      map.on('click', function(e) {
        // 获取点击位置坐标
        const { lat, lng } = e.lnglat
        // 设置标记点
        const marker = new AMap.Marker({
          position: new AMap.LngLat(lng, lat),
          icon: that.positionImage,
          anchor: 'bottom-center'
        })
        map.remove(that.marker)
        map.add(marker)
        that.marker = marker
        geocoder.getAddress([lng, lat], function(status, result) {
          address = result.regeocode.formattedAddress
          that.showsearchResult = false
          that.$emit('center', { lng, lat, address })
        })
      })
    }
  }
}
</script>
 
<style lang="css">
.m-map {
  min-width: 500px;
  min-height: 300px;
  position: relative;
}
.m-map .map {
  width: 100%;
  height: 100%;
}
.m-map .search {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 285px;
  z-index: 1;
}
.m-map .search input {
  width: 220px;
  border: 1px solid #ccc;
  height: 30px;
  box-sizing: border-box;
  line-height: 20px;
  padding: 5px;
  outline: none;
}
.m-map .search button {
  line-height: 26px;
  background: #fff;
  border: 1px solid #ccc;
  width: 50px;
  text-align: center;
}
.m-map .result {
  max-height: 300px;
  overflow: auto;
  margin-top: 10px;
}
#panel {
  position: absolute;
  top: 50px;
  left: 10px;
  width: 270px;
  z-index: 1;
}
.tool_search_result {
  position: absolute;
  top: 40px;
  left: 10px;
  width: 270px;
  height: 300px;
  overflow: auto;
  z-index: 1;
  border: 1px solid rgb(175, 175, 173);
  border-top: none;
  background: #fff;
  opacity: 0.8;
  bottom: auto;
  z-index: 12;
  position: absolute;
  text-align: left;
  font-size: 14px;
 
}
.tool_search_result ul {
  width: 100%;
  list-style: none;
  margin: 0;
  padding: 0;
}
.tool_search_result ul li {
  font-size: 12px;
  cursor: pointer;
  color: rgb(23, 40, 75);
  text-align: center;
  width: 100%;
  line-height: 2;
  padding: 5px 10px;
  box-sizing: border-box;
  border-bottom: 1px dashed rgb(170, 170, 172);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tool_search_result ul li:last-child {
  border: none;
}
</style>