addPoi.vue
17.3 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<template>
<v-full-layout ref="vFullLayout">
<template slot="main">
<div
ref="map"
id="map"
class="map-container"
v-loading="mapLoading"
></div>
<!-- 地图工具 开始 -->
<fence-map-controll
@onRefreshMap="refreshMap"
:isModalMap="true"
></fence-map-controll>
<!-- 地图工具 end -->
<!-- 地图搜索工具 开始 -->
<div class="map-seacrh-wrap">
<el-input
placeholder="请输入搜索地址"
id="poiMapSearch"
size="small"
maxlength="40"
v-model="poiMapSearch"
:disabled="readonlyStatus"
>
<template slot="prepend">搜索</template>
</el-input>
</div>
<!-- 地图搜索工具 end -->
<!-- 底部 开始 -->
<div class="bottom_list_hide" v-show="hideFooter" @click="onHide">
<i class="el-icon-arrow-up"></i>
<span>显示</span>
</div>
<div class="bottom_list" v-show="!hideFooter">
<div class="head">
<div class="left">
<span>POI基础信息</span>
</div>
<div class="right">
<a href="javascript:void(0)" class="link" @click="onHide">收起</a>
</div>
</div>
<div class="bottom_content">
<el-form
ref="poiForm"
:model="form"
:rules="formRules"
label-width="100px"
class="addPoiForm"
>
<div class="form_list">
<el-form-item label="POI名称:" prop="poiName">
<el-input v-model="form.poiName" maxlength="20"></el-input>
</el-form-item>
<el-form-item label="POI类型:" prop="poiType">
<el-select v-model="form.poiType" class="small_select">
<el-option label="黑区圆心" :value="1"></el-option>
<el-option label="二押点" :value="2"></el-option>
<el-option label="异常聚集点" :value="3"></el-option>
</el-select>
</el-form-item>
<div class="lnglat_box">
<el-form-item label="经度:" prop="lng">
<el-input v-model="form.lng" disabled />
</el-form-item>
<el-form-item label="维度:" prop="lat">
<el-input v-model="form.lat" disabled />
</el-form-item>
</div>
</div>
<div class="form_list">
<el-form-item label="描述:">
<el-input
type="textarea"
v-model="form.remark"
rows="5"
resize="none"
maxlength="200"
show-word-limit
></el-input>
</el-form-item>
</div>
</el-form>
<div class="btn_box">
<el-button type="primary" :loading="aloading" @click="saveInfo"
>保存</el-button
>
<el-button type="default" @click="goListPage">取消</el-button>
</div>
</div>
</div>
<!-- 底部 end -->
</template>
</v-full-layout>
</template>
<script>
import mapService from "@/utils/mapBService.js";
import "@/utils/mapEvent.js";
import { STATICIMAGE } from "@/constant";
import FenceMapControll from "./FenceMapControll";
import EventBus from "@/event-bus";
import { showToast, deepClone, showModal } from "@/utils/utils.js";
import {
ADD_POI_INFO,
GET_POI_TRACK,
DELETE_POI_INFO,
UPDATE_POI_INFO,
CHECK_POI_NAME,
GET_POI_INFO,
} from "@/api/services/poi";
export default {
components: {
FenceMapControll,
},
data() {
return {
map: null,
mapLoading: true,
form: {},
formRules: {
poiName: [
{ required: true, message: "请输入POI名称", trigger: "blur" },
{ validator: this.checkName, trigger: "blur" },
],
poiType: [
{ required: true, message: "请选择POI类型!", trigger: "change" },
],
lng: [
{
required: true,
message: "经纬度不能为空,请在地图中标注!",
trigger: "blur",
},
],
lat: [
{
required: true,
message: "经纬度不能为空,请在地图中标注!",
trigger: "blur",
},
],
},
markers: {},
infowins: {},
hideFooter: false,
poi: {},
poiMapSearch: "",
poiMarker: null, // 通过搜索、右键创建的点标记
aloading: false,
trackList: [], // 历史poi点
poiInfoWindow: null, // 当前的弹框
markerList: {}, // poi点标记列表
showMapText: false, //是否地图展示道路、标题
};
},
mounted() {
console.log(STATICIMAGE);
this.initBMap();
EventBus.$on("uiMap.poi.marker.delete", this.removePoi);
},
methods: {
async initBMap() {
this.mapObj = await mapService.init("map");
this.mapLoading = false;
window.AMap = this.mapObj;
this.autoComplete = AMap.invoke("Autocomplete", {
input: "poiMapSearch",
});
AMap.invoke("LocalCity", (res) => {
AMap.invoke('panTo',res.center)
});
AMap.invoke("autocompleteShow");
AMap.invoke("autocompleteAddEventListener", "onconfirm", (e) => {
let _poi = e.item.value;
// this.$set(this.form, "address", this.poi.address);
let _params = {};
_params.lng = _poi.location.lng;
_params.lat = _poi.location.lat;
this.addPoiMarker(_params, "search");
// this.$refs.poiMapSearch.focus();
});
this.initMapEvent();
this.getPoiTrack();
},
// 创建地图右键菜单
initMapEvent() {
const self = this;
const mapInstance = mapService.mapInstance;
var contextMenu = AMap.invoke("ContextMenu");
let _position = "";
contextMenu.addItem(
AMap.invoke("MenuItem", "设置POI点", function () {
self.addPoiMarker(_position);
})
);
AMap.invoke("mapAddEventListener", "rightclick", function (e) {
console.log(e)
AMap.invoke("addContextMenu", contextMenu);
_position = e.latlng;
});
},
// 批量添加海量点
addMassMarkers(arr) {
let _this = this;
let _query = _this.$route.query;
// let markers = [];
for (let i = 0, len = arr.length; i < len; i++) {
let k = arr[i];
if (k.lnglat) {
let m = k.lnglat.split(",");
let lnglatXY = mapService.getLngLat(m[0], m[1]);
// markers.push(lnglatXY);
let marker = mapService.createPoiMarker(lnglatXY, AMap);
marker.info=k
this.markers[i] = marker;
this.$set(this.markerList, k.id, marker);
// this.infowins[i] = infoWindow;
// AMap.invoke("addOverlay", marker);
// AMap.invoke("markerAddEventListener", "click", (e) => {
// let data = { ...e, ...k };
// this.markerEvent(data);
// });
}
}
this.createClusterer();
// AMap.invoke("setViewport", markers);
},
createClusterer() {
var cluster,
markers = [];
for (let idx in this.markers) {
markers.push(this.markers[idx]);
}
let icon=require('images/poi-marker-default.png')
this.cluster = AMap.invoke(
"Cluster",
markers,
(data) => {
console.log(data)
return {
point: [data.latLng.lng, data.latLng.lat],
properties: {
address: data.address,
near: data.near,
poiType: data.poiType,
// id: data.id,
// sn: data.sn,
// adcode: data.adcode,
marker: data,
},
};
},
{
clusterMinPoints: 2,
clusterMaxZoom: 18,
updateRealTime: true,
fitViewOnClick: true,
renderSingleStyle: {
type: Cluster.ClusterRender.WEBGL,
style: {
width: 14,
height: 19,
icon: icon
},
},
}
);
AMap.invoke("clusterSetData", this.cluster);
//click | mouseover | mouseout | change | destroy
AMap.invoke("clusterAddEventListener", "click", (e) => {
if (e.properties&&e.properties.marker) {
let data={...e.properties.marker,...e.properties.marker.info}
this.markerEvent(data);
// AMap.invoke(
// "infoWindowDisableCloseOnClick",
// e.properties.marker.openInfoWindow
// );
// AMap.invoke(
// "mapOpenInfoWindow",
// e.properties.marker.openInfoWindow,
// e.properties.marker.getPosition()
// );
}
});
// AMap.invoke("clusterAddEventListener", "mouseout", (e) => {
// AMap.invoke("mapCloseInfoWindow");
// });
},
// 地图右键添加poi点
addPoiMarker(options, type, isOpen) {
console.log(options,type, isOpen);
let _this = this;
let _map = _this.map;
let _InfoWindow = _this.poiInfoWindow;
if (_this.poiMarker) {
AMap.invoke("removeOverlay", _this.poiMarker);
_InfoWindow && AMap.invoke("mapCloseInfoWindow", _InfoWindow);
_this.poiMarker = null;
}
let _icon = AMap.invoke(
"Icon",
STATICIMAGE.POI_MARKER,
AMap.invoke("Size", 24, 24)
);
if (options.id) {
_icon = AMap.invoke(
"Icon",
STATICIMAGE.POI_MASS_ICON,
AMap.invoke("Size", 16, 21)
);
}
let lnglatXY=mapService.getLngLat(options.lng, options.lat);
let _marker=AMap.invoke('Marker', lnglatXY, {
icon:_icon
})
// let _marker = new AMap.Marker({
// icon: _icon,
// position: [options.lng, options.lat],
// extData: options,
// });
let _markerInfo=options
_markerInfo.latLng=mapService.getLngLat(options.lng, options.lat);
AMap.invoke("addOverlay", _marker);
this.markerEvent(_markerInfo);
// AMap.invoke("mapOpenInfoWindow", infoWindow,lnglat)
AMap.invoke("markerAddEventListener", "click", (e) => {
_markerInfo = { ...e, ..._markerInfo };
this.markerEvent(_markerInfo);
});
// let _markerInfo = _marker.getExtData();
// //_marker.infoWindow = mapService.createPoiMarkerInfowindow(options);
// _marker.on("click", function (e) {
// _this.markerEvent(_markerInfo);
// });
// _marker.setMap(_this.map);
// 历史marker存储起来,以id为键值,方便后续删除用
if (options.id) {
_this.$set(_this.markerList, options.id, _marker);
}
if (!options.id) {
_this.poiMarker = _marker;
_this.$set(_this.form, "lng", options.lng);
_this.$set(_this.form, "lat", options.lat);
_this.markerEvent(_markerInfo);
}
if (type && type == "search") {
_this.setCenter(options.lng, options.lat);
}
// 默认打开信息弹框
if (isOpen) {
_this.markerEvent(options);
_this.setCenter(options.lng, options.lat);
}
},
// marker点击事件
markerEvent(e) {
let _params = e;
this.setPoiInfo(e);
let point = e.latLng;
AMap.invoke("getLocation", point, (res) => {
let _pois = res.surroundingPois || [];
_params.address = res.address;
_params.near = _pois[0] ? _pois[0].title : "";
let infoWindow = mapService.createPoiMarkerInfowindow(_params);
this.poiInfoWindow = infoWindow;
AMap.invoke("mapOpenInfoWindow", infoWindow, point);
AMap.invoke("centerAndZoom",point,14)
});
},
// 设置poi信息
setPoiInfo(row) {
let _this = this;
_this.$set(_this.form, "lng", row.lng);
_this.$set(_this.form, "lat", row.lat);
_this.$set(_this.form, "remark", row.remark || "");
_this.$set(_this.form, "poiName", row.poiName || "");
_this.$set(_this.form, "poiType", row.poiType || "");
_this.$set(_this.form, "id", row.id || "");
_this.$set(_this.form, "enableStatus", row.enableStatus || "");
setTimeout(function () {
_this.$refs.poiForm.clearValidate();
}, 100);
},
// 隐藏
onHide() {
this.hideFooter = !this.hideFooter;
},
// 返回列表页面
goListPage() {
let path = `/trialRun#/poi/index`;
history.pushState({}, path, path);
},
// 获取参数
getParams() {
let params = {};
let m = deepClone(this.form);
params.poiName = m.poiName;
params.poiType = m.poiType;
params.lnglat = `${m.lng},${m.lat}`;
params.remark = m.remark;
if (m.id) {
params.id = m.id;
params.enableStatus = m.enableStatus;
}
return params;
},
// 保存信息
saveInfo() {
let _this = this;
_this.$refs.poiForm.validate((valid) => {
if (!valid) {
return false;
}
let _params = _this.getParams();
let _callBack = ADD_POI_INFO;
if (_params.id) {
_callBack = UPDATE_POI_INFO;
}
_this.aloading = true;
_callBack(_params).then((res) => {
console.log(res);
_this.aloading = false;
if (res.code == 200) {
showToast({
message: "保存成功",
type: "success",
});
_this.goListPage();
}
});
});
},
// 删除poi点
removePoi(row) {
let _this = this;
let _message = "确认删除该POI信息吗?";
let _typeList = {
1: "黑区圆心",
2: "二押点",
3: "异常聚集点",
};
if (row.poiName) {
_message = `确认删除【${row.poiName}】的【${
_typeList[row.poiType]
}】POI信息吗?`;
}
showModal({
message: _message,
type: "warning",
showCancelButton: true,
success: function () {
_this.removeRequest(row);
},
});
},
// 删除请求
removeRequest(row) {
let _this = this;
let _map = this.map;
let _params = {};
let _InfoWindow = this.poiInfoWindow;
// id不存在,临时的poi点,删除marker就行了
if (!row.id) {
AMap.invoke('removeOverlay', _this.poiMarker)
_InfoWindow && AMap.invoke('mapCloseInfoWindow', _InfoWindow);
_this.form = {};
setTimeout(function () {
_this.$refs.poiForm.clearValidate();
}, 100);
return false;
}
_params.id = row.id;
DELETE_POI_INFO(_params).then((res) => {
if (res.code == 200) {
// console.log(this.markerList,row)
let _marker = _this.markerList[row.id];
AMap.invoke('removeOverlay', _marker)
// AMap.invoke(
// "infoWindowDisableCloseOnClick",
// _InfoWindow
// );
_InfoWindow && AMap.invoke('mapCloseInfoWindow', _InfoWindow);
showToast({
message: "删除成功",
type: "success",
});
_this.form = {};
setTimeout(function () {
_this.$refs.poiForm.clearValidate();
}, 100);
}
});
},
// 获取历史poi点
getPoiTrack() {
let _this = this;
let _query = _this.$route.query;
GET_POI_TRACK().then((res) => {
console.log(res,'GET_POI_TRACK')
if (res.code == 200) {
let _list = res.data || [];
_this.addMassMarkers(_list);
// 编辑页面
if (_query.id) {
_this.getInfo();
}
_this.trackList = res.data || [];
// _this.showRoadTitle();
}
});
},
// 获取poi详情接口
getInfo() {
let _this = this;
let _query = _this.$route.query;
let _params = {};
_params.id = _query.id;
GET_POI_INFO(_params).then((res) => {
console.log(res);
let _obj = res.data || {};
if (_obj.lnglat) {
let arr = _obj.lnglat.split(",");
_obj.lng = arr[0];
_obj.lat = arr[1];
}
console.log(_obj);
_this.addPoiMarker(_obj, "", true);
});
},
// 校验名称
checkName(rule, value, callback) {
let _params = {};
_params.poiName = value;
if (this.form.id) {
_params.id = this.form.id;
}
CHECK_POI_NAME(_params).then((res) => {
if (res.code == 200) {
callback();
} else {
callback(new Error(res.message));
}
});
},
// 设置地图中心点
setCenter(lng, lat) {
let _this = this;
let _map = _this.map;
if (_map) {
console.log(lng, lat);
setTimeout(function () {
let lnglatXY = mapService.getLngLat(lng, lat);
AMap.invoke('panTo',lnglatXY)
}, 1000);
}
},
// 展示隐藏地图标题
showRoadTitle() {
let _road = new AMap.TileLayer.RoadNet();
let _title = new AMap.TileLayer();
this.map.add([_title]);
},
},
};
</script>
<style lang="scss" scoped>
@import "../../assets/scss/addPoi.scss";
</style>