fence.js
11.6 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
import EventBus from '@/event-bus'
import mapService from './mapService.js'
import MAPGEO from './mapgeo'
//电子围栏
const fence = {
polygons: [],
circle: null,
isEditMode: true,
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) {
this.polygonPath = data.path;
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) {
this.polygons && MAP.remove(this.polygons);
this.circle && MAP.remove(this.circle);
this.circleEditor && this.circleEditor.close();//MAP.remove(this.circleEditor);
this.polygons = [];
this.polyEditor && this.polyEditor.close()
this.mouseTool && this.mouseTool.close()
this.polygon && MAP.remove(this.polygon)
}
//this.clearTimeoutIds();
},
// 行政区域
drawDistrict(data) {
const MAP = mapService.map;
this.destroy();
if (!data || data.length <= 0) {
return false;
}
var opts = {
subdistrict: 0, //返回下一级行政区
extensions: 'all',
showbiz: false //最后一级返回街道信息
};
let oParams = data[data.length - 1];
//data == "中国" ? delete opts.extensions : delete opts.showbiz;
let district = new AMap.DistrictSearch(opts);
switch (data.length) {
case 1:
district.setLevel('province');
break;
case 2:
district.setLevel('city');
break;
default:
district.setLevel('district');
break;
}
//EventBus.$emit('uiMap.fence.loading', true);
//行政区查询
district.search(oParams, function (status, result) {
console.log(result)
const dst = result.districtList[0];
var bounds = dst.boundaries;
let lng = dst.center.lng;
let lat = dst.center.lat;
if (bounds) {
for (var i = 0, l = bounds.length; i < l; i++) {
//生成行政区划polygon
var polygon = new AMap.Polygon({
map: MAP,
strokeWeight: 1,
path: bounds[i],
fillOpacity: 0.7,
fillColor: '#CCF3FF',
strokeColor: '#CC66CC',
});
fence.polygons.push(polygon);
}
let center = mapService.getLngLat(lng, lat);
mapService.setFitView();
//EventBus.$emit('uiMap.fence.loading', false);
}
});
},
createFenceCircle(data) {
let _this = this
const MAP = mapService.map;
_this.destroy();
let mapInterval = setInterval(() => {
if (MAP) {
clearInterval(mapInterval)
console.log(data)
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
}
_this.circle = new AMap.Circle({
map: MAP,
center: mapService.getLngLat(lng, lat),
radius: data.radius * 1,
strokeColor: "#F33",
strokeOpacity: 1,
strokeWeight: 3,
fillColor: "ee2200",
fillOpacity: 0.35,
zIndex: 0,
});
_this.circleEditor = new AMap.CircleEditor(MAP, _this.circle);
_this.circleEditor.on('adjust', function (e) {
console.log('监听圆心改变', e)
EventBus.$emit('uiMap.fence.onFenceRadiusChange', e.radius / 1)
})
_this.circleEditor.on('move', function (e) {
EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
lng: e.lnglat.lng,
lat: e.lnglat.lat
});
})
if (_this.isEditMode) {
_this.circleEditor.open();
} else {
_this.circleEditor.close();
}
mapService.setFitView()
MAP.setZoom(14);
_this.bindContextMenu()
}
}, 100);
},
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 MAP = mapService.map;
//MAPGEO.transferAddressByGroup(this, ()=>{
//data.transferAddressGPS = this.data[0].transferAddress;
// if(this.data.length > 1){
// data.transferAddressBase = this.data[1].transferAddress;
// }
console.log(data)
let marker = mapService.createCarMarker([data.lngGPS, data.latGPS], MAP);
let infoWindow = mapService.bindFenceInfoWindow(marker, MAP, data);
//});
},
drawPolygon() {
const MAP = mapService.map;
const self = this;
this.destroy();
if (this.polygonPath.length) {
self.polygon = new AMap.Polygon({
path: this.polygonPath,
strokeColor: "#FF33FF",
strokeWeight: 6,
strokeOpacity: 0.2,
fillOpacity: 0.4,
fillColor: '#1791fc',
zIndex: 50,
})
MAP.add(self.polygon)
MAP.setFitView([self.polygon])
self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
if (self.isEditMode) {
self.polyEditor.open()
} else {
self.polyEditor.close()
}
self.bindPolyEvent(self.polyEditor)
} else if (self.isEditMode) {
self.mouseTool = new AMap.MouseTool(MAP)
self.mouseTool.polygon({
strokeColor: "#FF33FF",
strokeOpacity: 1,
strokeWeight: 6,
strokeOpacity: 0.2,
fillColor: '#1791fc',
fillOpacity: 0.4,
// 线样式还支持 'dashed'
strokeStyle: "solid",
// strokeStyle是dashed时有效
// strokeDasharray: [30,10],
})
self.mouseTool.on('draw', function (event) {
// event.obj 为绘制出来的覆盖物对象
self.polygon = event.obj;
self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
self.polyEditor.open()
self.bindPolyEvent(self.polyEditor)
self.mouseTool.close()
self.emitPolyEvent()
})
}
},
bindPolyEvent(polyEditor) {
const self = this
polyEditor.on('addnode', function (event) {
console.log('触发事件:addnode')
self.emitPolyEvent()
})
polyEditor.on('adjust', function (event) {
console.log('触发事件:adjust')
self.emitPolyEvent()
})
polyEditor.on('removenode', function (event) {
console.log('触发事件:removenode')
self.emitPolyEvent()
})
polyEditor.on('end', function (event) {
console.log('触发事件: end')
self.emitPolyEvent()
// event.target 即为编辑后的多边形对象
})
},
emitPolyEvent() {
let pathArr = this.polygon.getPath()
let resultArr = pathArr.map((item) => {
return [item.lng, item.lat]
})
this.polygonPath = resultArr
EventBus.$emit('uiMap.fence.polygonChange', {
polygonPath: this.polygonPath
});
console.log(resultArr.join('|'))
},
clearTimeoutIds() {
this.timeoutIds && this.timeoutIds.forEach((item) => {
item && window.clearTimeout(item);
});
this.timeoutIds = []
},
// 地图增加右键功能
bindContextMenu() {
const self = this;
const MAP = mapService.map;
var contextMenu = new AMap.ContextMenu();
contextMenu.addItem(
"设置围栏中心",
function () {
self.moveCenter();
},
0
);
//地图绑定鼠标右击事件——弹出右键菜单
MAP.on("rightclick", function (e) {
contextMenu.open(MAP, e.lnglat);
self.contextMenuPositon = e.lnglat;
});
},
// 改变圆心
moveCenter() {
this.lng = this.contextMenuPositon.lng;
this.lat = this.contextMenuPositon.lat;
let center = mapService.getLngLat(this.lng, this.lat);
this.circle.setCenter(center);
EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
lng: this.lng,
lat: this.lat,
});
},
// 更新围栏
updateFenceInfo(data) {
let _this = this;
const MAP = mapService.map;
let mapInterval = setInterval(function () {
if (MAP) {
clearInterval(mapInterval);
if (_this.fenceCircle) {
MAP.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);
}
console.log(data)
_this.createFenceCircle(data);
return false;
}
mapService.setFitView()
MAP.setZoom(14);
}
}, 100);
}
}
export default fence;