fence.js
12.9 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
/*
* @Author: 'wangzhihui' '1733904062@qq.com'
* @Date: 2024-11-28 16:37:15
* @LastEditors: 'wangzhihui' '1733904062@qq.com'
* @LastEditTime: 2024-12-30 14:58:58
* @FilePath: \oEshield\src\utils\fence.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import EventBus from '@/event-bus'
// import mapService from './mapService.js'
import mapService from './mapBService.js'
// import MAPGEO from './mapgeo'
import MAPGEO from './mapBgeo'
//电子围栏
const fence = {
polygons: [],
circle: null,
dist:null,
drawingManager:null,
isEditMode: true,
marker:'',
bindEvent() {
EventBus.$on('uiMap.fence.drawDistrict', (data) => {
this.drawDistrict(data)
});
EventBus.$on('uiMap.fence.createFenceCircle', (data) => {
if (('isEdit' in data) && !data.isEdit) {
this.isEditMode = false
} else {
this.isEditMode = true
}
this.createFenceCircle(data)
});
EventBus.$on('uiMap.fence.updateFenceRadius', (data) => {
console.log('监听围栏更新', data)
this.updateFenceInfo(data)
});
//this.circle.setRadius(data * 1);
EventBus.$on('uiMap.fence.createFenceMarker', (data) => {
this.createFenceMarker(data)
});
EventBus.$on('uiMap.fence.drawPolygon', (data) => {
if (data) {
if (Array.isArray(data)) {
this.polygonPath = data;
this.isEditMode = true;
} else if (('isEdit' in data) && !data.isEdit) {
let arr= data.path.map(item=>{
return mapService.getLngLat(item[0], item[1])
})
this.polygonPath =arr;
this.isEditMode = false;
}
}
this.drawPolygon()
});
EventBus.$on('uiMap.fence.destroy', (data) => {
this.polyEditor && this.polyEditor.close()
this.mouseTool && this.mouseTool.close()
});
},
destroy() {
const MAP = mapService.map;
if (MAP) {
// // MAP.invoke('removeOverlay',this.polygons)
// this.polygon &&MAP.invoke('removeOverlay',this.polygon);
// this.circle && MAP.invoke('removeOverlay',this.circle);
// this.drawingManager && MAP.invoke('removeOverlay',this.drawingManager);
AMap.invoke('clearOverlays');
AMap.invoke('addOverlay',this.marker)
//清除多边形
if( this.drawingManager&&this.drawingManager.getOverlays().length){
let overlays=this.drawingManager.getOverlays()[0]
AMap.invoke('removeOverlay',overlays);
}
if(document.getElementById('cancelOperate')){
document.getElementById('cancelOperate').click()
}
this.dist&& MAP.invoke('removeDistrictLayer', this.dist)
// this.polygons = [];
// this.polyEditor && this.polyEditor.close()
// this.mouseTool && this.mouseTool.close()
}
//this.clearTimeoutIds();
},
// 行政区域
drawDistrict(data) {
this.destroy();
if (!data || data.length <= 0) {
return false;
}
if ( this.dist) AMap.invoke('removeDistrictLayer', this.dist)
this.dist = AMap.invoke('DistrictLayer', {
name: `(${data[data.length-1]})`,
fillOpacity: 0.7,
fillColor: '#CCF3FF',
strokeColor: '#CC66CC',
viewport: true
});
AMap.invoke('addDistrictLayer', this.dist)
},
updateFenceCircle(data){
console.log(data)
let center
AMap.invoke('circleDisableEditing')
if( data.radius&&this.circle){
this.circle.setRadius(data.radius * 1)
}
if(data.lng&&data.lat){
center = mapService.getLngLat(data.lng, this.lat);
this.circle&& this.circle.setCenter(center);
}
center= this.circle&& this.circle.getCenter()
AMap.invoke('removeOverlay', this.circle)
AMap.invoke('addOverlay', this.circle)
AMap.invoke('circleEnableEditing')
AMap.invoke('panTo',center,true)
},
createFenceCircle(data) {
if(!this.circle){
let _this = this
const mapInstance= mapService.mapInstance
let mapInterval = setInterval(() => {
if (AMap) {
clearInterval(mapInterval)
let lng = data.lng || _this.lng
let lat = data.lat || _this.lat
if (data.lng) {
_this.lng = data.lng
}
if (data.lat) {
_this.lat = data.lat
}
const lnglat=mapService.getLngLat(lng, lat)
let options={
strokeColor: "#F33",
strokeOpacity: 1,
strokeWeight: 3,
fillColor: "ee2200",
fillOpacity: 0.35,
enableEditing:true
}
if(!_this.isEditMode) options.enableEditing=false
_this.circle=AMap.invoke('Circle',lnglat,data.radius * 1,options)
AMap.invoke('panTo',lnglat,true)
AMap.invoke('addOverlay',_this.circle)
AMap.invoke('setZoom',14)
_this.bindContextMenu()
AMap.invoke('circleAddEventListener','lineupdate',function(e){
let overlay=e.overlay
EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
lng:e.overlay.getCenter().lng,
lat:e.overlay.getCenter().lat
});
EventBus.$emit('uiMap.fence.onFenceRadiusChange', parseInt(overlay.getRadius() / 1))
})
}
}, 2000);
}else{
this.destroy();
this.updateFenceCircle(data)
}
},
createFenceMarker(data) {
this.data = [{
elng: data.lngGPS,
elat: data.latGPS
}];
if (data.lngBase && data.latBase) {
this.data.push({
elng: data.lngBase,
elat: data.latBase
});
}
const getLngLat=mapService.getLngLat(data.lngGPS,data.latGPS)
this.marker = mapService.createCarMarker(getLngLat, AMap);
let infoWindow = mapService.bindFenceInfoWindow( this.marker, AMap, data);
AMap.invoke('addOverlay',this.marker)
},
drawPolygon() {
const self = this;
this.destroy();
if (this.polygonPath.length) {
let options={
zIndex: 10,
strokeColor: "#FF33FF",
strokeWeight: 6,
strokeOpacity: 0.2,
fillOpacity: 0.4,
fillColor: '#1791fc',
enableEditing:true
}
if(!self.isEditMode){
options.enableEditing=false
}
self.polygon=AMap.invoke('Polygon',this.polygonPath,options)
//获取多边形的中间位置
let lnglat=self.polygon.getBounds().getCenter()
//地图的中心点更改为给定的点
AMap.invoke('panTo',lnglat,true)
AMap.invoke('polygonAddEventListener','lineupdate',function(e){
let pathArr = self.polygon.getPath()
self.emitPolyEvent(pathArr)
})
AMap.invoke('addOverlay', self.polygon)
AMap.invoke('setZoom',17)
} else if (self.isEditMode) {
var styleOptions = {
strokeColor: '#FF33FF', // 边线颜色
fillColor: '#1791fc', // 填充颜色。当参数为空时,圆形没有填充颜色
strokeWeight: 6, // 边线宽度,以像素为单位
strokeOpacity: 0.2, // 边线透明度,取值范围0-1
fillOpacity: 0.4 // 填充透明度,取值范围0-1
};
var labelOptions = {
borderRadius: '2px',
background: '#FF33FF',
border: '1px solid #E1E1E1',
color: '#703A04',
fontSize: '12px',
letterSpacing: '0',
padding: '5px'
};
self.drawingManager = AMap.invoke('DrawingManager', mapService.mapInstance, {
isOpen: true, // 是否开启绘制模式
enableDrawingTool: true,
enableCalculate: false, // 绘制是否进行测距测面
enableSorption: true, // 是否开启边界吸附功能
sorptiondistance: 20, // 边界吸附距离
circleOptions: styleOptions, // 圆的样式
polylineOptions: styleOptions, // 线的样式
polygonOptions: styleOptions, // 多边形的样式
rectangleOptions: styleOptions, // 矩形的样式
labelOptions: labelOptions, // label样式
})
AMap.invoke('drawingManagerAddEventListener','polygoncomplete',function(e){
let overlays=e
let pathArr = e.getPath()
console.log(pathArr)
// let resultArr = pathArr.map((item) => {
// return item.latLng
// })
self.emitPolyEvent(pathArr)
})
AMap.invoke('setDrawingMode', 'polygon')
}
},
bindPolyEvent(polyEditor) {
const self = this
polyEditor.on('addnode', function (event) {
self.emitPolyEvent()
})
polyEditor.on('adjust', function (event) {
self.emitPolyEvent()
})
polyEditor.on('removenode', function (event) {
self.emitPolyEvent()
})
polyEditor.on('end', function (event) {
self.emitPolyEvent()
// event.target 即为编辑后的多边形对象
})
},
emitPolyEvent(resultArr) {
this.polygonPath = resultArr
EventBus.$emit('uiMap.fence.polygonChange', {
polygonPath: this.polygonPath
});
},
clearTimeoutIds() {
this.timeoutIds && this.timeoutIds.forEach((item) => {
item && window.clearTimeout(item);
});
this.timeoutIds = []
},
// 地图增加右键功能
bindContextMenu() {
const self = this;
const mapInstance = mapService.mapInstance;
var contextMenu = AMap.invoke('ContextMenu');
contextMenu.addItem(
AMap.invoke('MenuItem',"设置围栏中心", function () {
self.moveCenter();
})
);
AMap.invoke('mapAddEventListener','rightclick',function (e) {
AMap.invoke('addContextMenu',contextMenu)
self.contextMenuPositon = e.latlng
})
},
// 改变圆心
moveCenter() {
const MAP = mapService.map;
AMap.invoke('circleDisableEditing')
this.circle&& this.circle.setCenter(this.contextMenuPositon);
AMap.invoke('removeOverlay', this.circle)
AMap.invoke('addOverlay', this.circle)
AMap.invoke('circleEnableEditing')
},
// 更新围栏
updateFenceInfo(data) {
let _this = this;
const MAP = mapService.map;
let mapInterval = setInterval(function () {
if (AMap) {
clearInterval(mapInterval);
if (_this.fenceCircle) {
AMap.remove(_this.fenceCircle)
}
_this.fenceType = data.fenceType;
_this.polyEditor && _this.polyEditor.close();
_this.mouseTool && _this.mouseTool.close();
// 更新圆形围栏
if (data.fenceType == 'circle') {
_this.fenceCircle = null;
if (data.lng) {
_this.lng = data.lng;
}
if (data.lat) {
_this.lat = data.lat;
}
if (data.radius) {
_this.radius = Number(data.radius);
}
// _this.createFenceCircle(data);
_this.updateFenceCircle(data)
return false;
}
// mapService.setFitView()
MAP.setZoom(14);
AMap.invoke('setZoom',14)
}
}, 100);
}
}
export default fence;