index.vue
13.2 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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<template>
<div class="map-container">
<div class="map-box">
<el-select v-model="input" class="map-input" filterable remote reserve-keyword placeholder="请输入" :remote-method="remoteMethod" @change="handleChangeAdr" :loading="loading">
<el-option v-for="item in adrList" :key="item.id" :label="item.address" :value="item.id"></el-option>
</el-select>
<div class="icon-card">
<div class="icon-item" v-for="(ctr, index) in controlList" @click="controlHandle(index, ctr.icon)" :key="index">
<img class="control-icon" :src="require(`@/assets/${ctr.icon}.png`)" />
{{ ctr.name }}
</div>
</div>
</div>
<!-- 地图 -->
<div id="container" :style="{ width: width, height: height }"></div>
</div>
</template>
<script>
let content = [],
contextMenu,
circle
import AMapLoader from '@amap/amap-jsapi-loader' //按需引入依赖
export default {
data() {
return {
input: [],
adrList: [],
placeSearch: '',
map: '',
address: '',
markerData:{},
contextMenuPositon: '', //右键菜单位置
controlList: [
{
icon: 'moon',
name: '卫星'
},
{
icon: 'ranging',
name: '测距'
},
{
icon: 'location',
name: '定位'
},
{
icon: 'magnify',
name: '放大'
},
{
icon: 'reduce',
name: '缩小'
}
],
activeIndex: ''
}
},
props: {
id: {
type: Number,
default: 0
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '100%'
}
},
mounted() {
;(this.merchantId = this.$globalState.session.merchantId), this.initMap()
},
methods: {
initMap() {
AMapLoader.load({
key: '44cc16e43ad7ed3358df507bd0618181', // 申请好的Web端开发者Key,首次调用 load 时必填
version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [
//需要使用的插件
'AMap.ToolBar',
'AMap.Scale',
'AMap.Geolocation',
'AMap.PlaceSearch',
'AMap.AutoComplete',
'AMap.Geocoder',
'AMap.CitySearch',
'AMap.RangingTool'
],
resizeEnable: true
})
.then(AMap => {
const that = this
that.AMap = AMap
that.map = new AMap.Map('container', {
//设置地图容器id
viewMode: '3D', //是否为3D地图模式
zoom: 12,
autoFitView: true,
resizeEnable: true,
cursor: 'move' //初始化地图级别
})
//构造地点查询类
that.placeSearch = new AMap.PlaceSearch({
pageSize: 8, // 单页显示结果条数
pageIndex: 1, // 页码
citylimit: false, //是否强制限制在设置的城市内搜索
// panel: "panel", // 结果列表将在此容器中进行展示。
autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
renderStyle: 'default'
})
// that.map.on('click', this.getAddress)
that.getContextMenu()
})
.catch(e => {
console.log(e)
})
},
initMarket(marker) {
let { map, AMap, contextMenuPositon, address,markerData } = this,
that = this,
infoWindowContent,
infoWindow
if (marker) {
marker.on('click', function (e) {
console.log('点击', e)
markerData=marker.getExtData()
that.contextMenuPositon = e.lnglat
that.getAddress()
infoWindowContent = `<div class="custom-infowindow input-card">
<div class="title">新增标记</div>
<div class="custom-item">
<div class="item">
<span>经度</span>
<span class="item-text" >${contextMenuPositon.lng}</span>
</div>
<div class="item">
<span>纬度</span>
<span class="item-text" >${contextMenuPositon.lat}</span>
</div>
<div class="item">
<span>车位编号</span>
<span class="item-text" >${markerData.parkingNumber}</span>
</div>
<div class="item">
<span>经纬度解析</span>
<span class="item-text" >${address}</span>
</div>
</div>
<div>
<div class="footer">
<div class="cancel">取消</div>
<div class="del">删除</div>
</div>
</div>`
infoWindow = new AMap.InfoWindow({
content: infoWindowContent
})
infoWindow.open(map, contextMenuPositon)
that.bindEvent(infoWindow)
})
}
// marker = new AMap.Marker({
// map: map,
// clickable:true
// })
},
getContextMenu() {
let { map, AMap } = this,
that = this
content.push("<div class='info context_menu'>")
content.push(" <p class='marker' id='marker'><span>+</span>新增车位标记</p>")
content.push('</div>')
//通过content自定义右键菜单内容
contextMenu = new AMap.ContextMenu({ isCustom: true, content: content.join('') })
//地图绑定鼠标右击事件——弹出右键菜单
map.on('rightclick', function (e) {
contextMenu.open(map, e.lnglat)
that.contextMenuPositon = e.lnglat //右键菜单位置
that.getAddress()
that.marketEvent()
})
},
marketEvent() {
let marker = document.getElementById('marker')
marker.addEventListener('click', () => {
this.addMarkerMenu()
})
},
addMarkerMenu() {
let { map, AMap, contextMenuPositon, address } = this,
infoWindow,
infoWindowContent,
geocoder
infoWindowContent = `<div class="custom-infowindow input-card">
<div class="title">新增标记</div>
<div class="custom-item">
<div class="item">
<span>经度</span>
<span class="item-text" >${contextMenuPositon.lng}</span>
</div>
<div class="item">
<span>纬度</span>
<span class="item-text" >${contextMenuPositon.lat}</span>
</div>
<div class="item">
<span>车位编号</span>
<span class="item-text" >124.214.124</span>
</div>
<div class="item">
<span>经纬度解析</span>
<span class="item-text" >${address}</span>
</div>
</div>
<div>
<div class="footer">
<div class="cancel">取消</div>
<div class="confirm">确定</div>
</div>
</div>`
infoWindow = new AMap.InfoWindow({
content: infoWindowContent
})
contextMenu.close()
infoWindow.open(map, contextMenuPositon)
this.bindEvent(infoWindow)
},
bindEvent(infoWindow) {
let confirmBtn = document.getElementsByClassName('confirm')[0],
cancelBtn = document.getElementsByClassName('cancel')[0],
delBtn = document.getElementsByClassName('del')[0]
confirmBtn &&
confirmBtn.addEventListener('click', () => {
this.confirm(infoWindow)
})
cancelBtn &&
cancelBtn.addEventListener('click', () => {
this.cancel(infoWindow)
})
delBtn &&
delBtn.addEventListener('click', () => {
this.del(infoWindow)
})
},
confirm(infoWindow) {
let {
AMap,
map,
id,
merchantId,
contextMenuPositon: { lng, lat }
} = this,
marker
let params = {
id,
parkingAddressLng: lng,
parkingAddressLat: lat,
merchantId
}
this.$apis.parkingSpace.addFlagparkingSpace(params).then(res => {
if (res) {
marker = new AMap.Marker({
icon: require('@/assets/default.png'),
position: [lng, lat],
anchor: 'bottom-left',
extData: {
id
},
style: { width: '48px', height: '48px' }
})
marker.setMap(map)
this.initMarket(marker)
infoWindow.close()
this.$message.success('车位标记成功')
}
})
},
cancel(infoWindow) {
infoWindow.close()
},
del(infoWindow) {
infoWindow.close()
},
remoteMethod(query) {
if (query == '') {
return true
}
let _this = this
_this.placeSearch.search(query, function (status, result) {
if (status === 'complete' && result.poiList.pois.length) {
_this.adrList = result.poiList.pois
console.log(_this.adrList)
}
})
},
getAddress(e) {
let { map, AMap, contextMenuPositon } = this,
that = this,
geocoder
console.log(e)
if (e) {
circle = new AMap.Circle({
map: map,
center: e.location,
radius: 10, //半径
borderWeight: 1,
// 线样式还支持 'dashed'
fillColor: '#1791fc',
zIndex: 50
})
circle.setMap(map)
// 缩放地图到合适的视野级别
map.setFitView([circle])
this.shroudContextMenu(e.location)
} else {
//获取地址
geocoder = new AMap.Geocoder({
radius: 1000 //范围,默认:500
})
geocoder.getAddress([contextMenuPositon.lng, contextMenuPositon.lat], function (status, result) {
if (status === 'complete' && result.regeocode) {
that.address = result.regeocode.formattedAddress
}
})
}
},
shroudContextMenu(lnglat) {
let { map, AMap } = this,
that = this
//绑定鼠标右击事件——弹出右键菜单
circle.on('rightclick', function (e) {
contextMenu.open(map, e.lnglat)
that.contextMenuPositon = e.lnglat
that.getAddress()
that.marketEvent()
})
},
handleChangeAdr(val) {
if (val) {
var location = this.adrList.find(item => item.id == val)
this.getAddress(location)
this.input = ''
this.adrList = []
}
},
controlHandle(index, event) {
this[event + 'Handle'](index)
this.activeIndex = index
},
//卫星
moonHandle(index) {
let { AMap, map, activeIndex } = this
if (activeIndex === index) {
new AMap.TileLayer({
map
})
return
}
new AMap.TileLayer.Satellite({
map
})
},
//测距
rangingHandle(index) {
let { AMap, map, activeIndex } = this,
ruler
ruler = new AMap.RangingTool(map)
if (activeIndex === index) {
ruler.turnOff()
return
}
ruler.turnOn()
},
//定位
locationHandle() {
let { AMap, map } = this,
geolocation
geolocation = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:5s
position: 'RB', //定位按钮的停靠位置
offset: [10, 20], //定位按钮与设置的停靠位置的偏移量,默认:[10, 20]
zoomToAccuracy: true,
showButton: false //定位成功后是否自动调整地图视野到定位点
})
map.addControl(geolocation)
},
//放大
magnifyHandle() {
let { map } = this
map.zoomIn()
},
//缩小
reduceHandle() {
let { map } = this
map.zoomOut()
}
}
}
</script>
<style scoped lang="scss">
.map-container {
position: relative;
width: 100%;
height: calc(100vh - 80px);
.map-box {
width: 100%;
position: absolute;
top: 20px;
display: flex;
align-items: center;
padding: 0 18%;
box-sizing: border-box;
}
.map-input {
width: 40%;
z-index: 2;
margin-right: 16px;
}
#container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
width: 100%;
}
.icon-card {
z-index: 2;
// width: 100%;
height: 38px;
background: #ffffff;
box-shadow: 0 3px 6px -4px #0000001f, 0 6px 16px 0 #00000014, 0 9px 28px 8px #0000000d;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 10px;
box-sizing: border-box;
cursor: pointer;
.icon-item {
font-size: 14px;
color: #666666;
display: flex;
align-items: center;
justify-items: center;
line-height: 22px;
border-right: 1px solid #f0f1f7;
padding-right: 10px;
box-sizing: border-box;
&:last-child {
border-right: none;
}
img {
width: 18px;
height: 18px;
margin: 0 6px 0 10px;
}
}
}
}
</style>