fence.js
8.64 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
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)=>{
this.circle.setRadius(data*1);
mapService.setFitView()
});
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;
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();
var opts = {
subdistrict: 1, //返回下一级行政区
extensions: 'all',
showbiz:false //最后一级返回街道信息
};
data == "中国"?delete opts.extensions :delete opts.showbiz;
let district = new AMap.DistrictSearch(opts);
district.setLevel('district');
EventBus.$emit('uiMap.fence.loading', true);
//行政区查询
district.search(data.toString(), function(status, 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){
const MAP = mapService.map;
this.destroy();
this.circle = new AMap.Circle({
map: MAP,
center: mapService.getLngLat(data.lng, data.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);
AMap.event.addListener(this.circleEditor, 'adjust', (e)=>{
EventBus.$emit('uiMap.fence.onFenceRadiusChange', e.radius/1)
/*this.form.radius = e.radius/1;
this.$emit('onUpdateMapData',{
lng: this.form.lng,
lat: this.form.lat,
address: this.form.transferAddr,
radius: this.form.radius
});*/
//this.$refs.fenceMapForm.validateField('radius');
});
AMap.event.addListener(this.circleEditor, 'move', (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);
},
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;
}
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 = []
}
}
export default fence;