forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlanesMonitor.html
More file actions
565 lines (507 loc) · 20.2 KB
/
PlanesMonitor.html
File metadata and controls
565 lines (507 loc) · 20.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>航班监控</title>
<script type="text/javascript"
include="jquery,bootstrap,moment,bootstrap-datetimepicker,bootstrap-select,geohash,randomcolor"
src="../js/include-web.js"></script>
<style>
.bootstrap-select {
width: 168px !important;
}
.form-group label {
width: 90px;
}
.tips {
z-index: 1000;
}
</style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%;border: 1px solid #dddddd"></div>
<script type="text/javascript" include="mapv,elasticsearch" src="../../dist/include-leaflet.js"></script>
<script type="text/javascript">
var map;
var timeControl,
liveESService,//实时点查询的ESService
trackESService,//历史轨迹查询的ESService
liveRenderer,//实时点层
trackRenderer,//历史轨迹层
liveDataSet,//渲染用实时点数据集
trackDataSet,//渲染用历史轨迹线数据集
planeStyles,
planeLabels,//航班信息提示框集合
geoFenceLabels,//地理围栏信息提示框集合
geoFenceLayer,
dataUrl = "http://117.122.248.69:9200";
var flightIdens = [
'CCA4189', 'CCA4187', 'CBJ5531', 'CSC8747', 'TBA9879',
'CCA4113', 'CES2251', 'CCA4521', 'CSC8927', 'CSC8811',
'CSC8857', 'UEA2711', 'UEA2205', 'CCA4439', 'CCA415',
'CSC8719', 'CSC8555', 'CCA4211', 'HDA825'
];
init();
function init() {
initMap();
liveESService = new SuperMap.ElasticSearch(dataUrl, {
change: renderLive,
outOfGeoFence: renderOutOfGeoFenceLabel,
openGeoFence: true
});
trackESService = new SuperMap.ElasticSearch(dataUrl, {
change: renderTrack,
openGeoFence: true
});
planeStyles = initPlaneStyles();
}
//开始播放
function start() {
var options = getControlOptions();
setGeoFence();
if (!timeControl) {
timeControl = new SuperMap.TimeFlowControl(loadLiveData, options);
} else {
timeControl.updateOptions(options);
}
timeControl.start();
}
//暂停播放
function pause() {
timeControl.pause();
}
//停止播放
function stop() {
timeControl.stop();
clearAll();
}
//时间控制器回调参数,即每次刷新时执行的操作,此处为向服务器请求数据并绘制。实时刷新执行。
function loadLiveData(currentTime) {
//第一次请求之前先查询轨迹
if (currentTime === timeControl.getStartTime()) {
loadTrackData();
}
var datetime = moment(currentTime).format("YYYY-MM-DDTHH:mm:ss") + "Z";
var options = getServiceParamOptions();
var liveParameters = [];
for (var i = 0; i < options.flightIds.length; i++) {
var code = options.flightIds[i];
if (code === 'ZUUU') {
continue;
}
liveParameters.push({index: "flights", type: "flight_utc"});
liveParameters.push({
"query": {
"bool": {
"must": {"match": {"ident": code}},
"filter": {"match": {"datetime": datetime}},
}
},
"sort": {"time-ms": {"order": "ASC"}},
"from": 0,
"size": 20
});
}
liveESService.msearch({body: liveParameters});
updateProgress(datetime);
}
//第一次请求先查询历史轨迹数据,只调用一次
function loadTrackData() {
var start = timeControl.getStartTime();
var end = timeControl.getEndTime();
var options = getServiceParamOptions();
var trackParameters = [];
for (var i = 0; i < options.flightIds.length; i++) {
var code = options.flightIds[i];
if (code === 'ZUUU') {
continue;
}
trackParameters.push({index: "flights", type: "flight_utc"});
trackParameters.push({
"query": {
"bool": {
"must": {"match": {"ident": code}},
"filter": {
"range": {
"time-ms": {
"gte": start,
"lte": end
}
}
},
}
},
"sort": {"time-ms": {"order": "ASC"}},
"from": 0,
"size": 10000
});
}
trackESService.msearch({body: trackParameters});
}
//渲染轨迹图层
function renderTrack(result) {
result = result || {};
var data = createTrackRendererData(result);
if (data.length < 1) {
return;
}
trackDataSet = new mapv.DataSet(data);
var options = {
zIndex: 1,
strokeStyle: 'rgba(255, 250, 50, 0.8)',
shadowColor: 'rgba(255, 250, 50, 1)',
shadowBlur: 1.5,
lineWidth: 1,
draw: 'simple'
};
trackRenderer = L.supermap.mapVLayer(trackDataSet, options).addTo(map);
}
//解析轨迹查询结果数据为mapv数据
function createTrackRendererData(results) {
var data = [];
results.map(function (result) {
var coordinates = [];
var ident;
result.hits.hits.map(function (source) {
content = source._source;
if (!ident) {
ident = content.ident;
}
coordinates.push([content.x, content.y]);
});
data.push({
geometry: {
type: 'LineString',
coordinates: coordinates
}
});
});
return data;
}
//渲染实时点数据
function renderLive(result) {
if (timeControl && !timeControl.getRunning()) {
return;
}
result = result || {};
var data = createLiveRendererData(result);
if (data.length < 1) {
return;
}
updateDataSet(data);
if (!liveRenderer) {
liveRenderer = L.supermap.mapVLayer(liveDataSet, {
zIndex: 800,
draw: 'icon'
}).addTo(map);
} else {
liveRenderer.update({data: liveDataSet});
}
renderLiveLabel(data);
}
//解析点查询结果数据为mapv数据
function createLiveRendererData(results) {
var data = [];
results.map(function (result) {
result.hits.hits.map(function (source) {
var content = source._source;
data.push({
geometry: {
type: 'Point',
coordinates: [content.x, content.y],
},
deg: content.direction,
icon: planeStyles[content.ident].img,
ident: content.ident,
content: content
});
});
});
return data;
}
//更新点数据集
function updateDataSet(data) {
if (!liveDataSet) {
liveDataSet = new mapv.DataSet(data);
return;
}
var innerData = liveDataSet.get();
var dataLen = data.length;
for (var i = 0; i < innerData.length; i++) {
if (i < dataLen && data[i].ident === innerData[i].ident) {
innerData[i] = data[i];
}
}
liveDataSet.set(innerData);
}
//更新实时点的提示框
function renderLiveLabel(data) {
planeLabels = planeLabels || {};
for (var i = 0; i < data.length; i++) {
var content = data[i].content;
var latlng = L.latLng(content.y, content.x);
var labelContent = "航班: " + content.ident + "<br>" +
"始发地: " + content.origin + "/" + content.originLabel + "<br>" +
"目的地: " + content.destination + "/" + content.destinationLabel + "<br>" +
"时间: " + content.datetime;
if (!planeLabels[content.ident]) {
planeLabels[content.ident] = L.popup({
closeOnClick: false,
autoPan: false,
maxWidth: 180,
offset: L.point(0, -1)
}).setContent(labelContent).setLatLng(latlng).addTo(map);
} else {
planeLabels[content.ident].setLatLng(latlng).setContent(labelContent);
}
}
}
//更新地理围栏提示框
function renderOutOfGeoFenceLabel(data) {
geoFenceLabels = geoFenceLabels || {};
var content = data.hits.hits[0]._source;
var latlng = L.latLng(content.y, content.x);
var labelContent = "<div style='color:red'>航班" + content.ident + "超出地理围栏</div>";
if (!geoFenceLabels[content.ident]) {
geoFenceLabels[content.ident] = L.popup({
closeOnClick: false,
autoPan: false,
maxWidth: 180,
className: "tips",
offset: L.point(0, -1)
}).setContent(labelContent).setLatLng(latlng).addTo(map);
} else {
geoFenceLabels[content.ident].setLatLng(latlng).setContent(labelContent).openOn(map);
}
}
//点击开始之后配置地理围栏
function setGeoFence() {
var geoFence = getGeoFence();
liveESService.setGeoFence(geoFence);
if (!geoFenceLayer) {
geoFenceLayer = L.circle(L.latLng(geoFence.center[1], geoFence.center[0]), {
radius: geoFence.radius,
weight: 1
}).addTo(map);
}
}
//初始化飞机的样式,颜色随机
function initPlaneStyles() {
var planes = {};
var colors = randomColor({
luminosity: 'bright',
hue: 'random',
alpha: 1,
count: flightIdens.length
});
flightIdens.map(function (ident, key) {
var svgUrl = "data:image/svg+xml;utf8,";
$.get('../data/plane.svg', function (svg) {
$(svg).children('svg').children('path').attr('fill', colors[key]);
svgUrl += $(svg).children('svg')[0].outerHTML;
var img = new Image();
img.src = svgUrl;
planes[ident] = {color: colors[key], img: img};
});
});
return planes;
}
//获取时间控件设置的参数
function getControlOptions() {
var startTime = $("#startTime").val();
var endTime = $("#endTime").val();
startTime = new Date(Date.parse(startTime.replace(/-/g, "/"))).getTime();
endTime = new Date(Date.parse(endTime.replace(/-/g, "/"))).getTime();
var speed = $("#speed").val();
speed = (speed > 0) ? speed : 1000;
speed = parseInt(speed);
var frequency = $("#frequency").val();
frequency = (frequency > 0) ? frequency : 1000;
frequency = parseInt(frequency);
return {
startTime: startTime,
endTime: endTime,
speed: speed,
frequency: frequency
}
}
//获取ES服务查询参数
function getServiceParamOptions() {
var flightIds = $("#flightIds").val();
return {
flightIds: flightIds
}
}
//获取地理围栏参数
function getGeoFence() {
var geoFence = $("#geoFence").val();
return JSON.parse(geoFence);
}
//更新当前时间界面
function updateProgress(currentTime) {
$("#progress").html(currentTime);
}
//获取默认地理围栏参数
function getDefaultGeoFence() {
return {
center: [103.9, 30.58],
radius: 200000,
unit: "degree"
}
}
//默认设置参数
function getDefaultControlOptions() {
var startMs = 1498781767725;
var endMs = 1498935332876;
var start = moment(startMs).format("YYYY-MM-DDTHH:mm:ss") + "Z";
var end = moment(endMs).format("YYYY-MM-DDTHH:mm:ss") + "Z";
return {
startTime: start,
endTime: end,
speed: 1000,
frequency: 100,
}
}
function initMap() {
if (!map) {
map = L.map('map', {
center: [34, 110],
maxZoom: 20,
minZoom: 3,
zoom: 5,
crs: L.CRS.EPSG3857
});
var attr = 'Data © <a href="https://www.elastic.co/products/elasticsearch" target="_blank">Elasticsearch</a> Map Data <span>© <a href="http://support.supermap.com.cn/product/iServer.aspx" target="_blank">SuperMap iServer</a></span> with <span>© <a href="http://iclient.supermap.io" target="_blank">SuperMap iClient</a></span>';
var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
var url = host + "/iserver/services/map-china400/rest/maps/ChinaDark";
L.supermap.tiledMapLayer(url, {attribution: attr}).addTo(map);
}
initTimeControlView();
}
//初始化时间控制控件,仅UI
function initTimeControlView() {
var control = L.control({position: "topright"});
control.onAdd = function () {
var me = this;
me._div = L.DomUtil.create('div', 'panel panel-primary controlPane');
me._div.style.width = "300px";
var titleDiv = $("<div class='panel-heading text-center' id='toggle' style='cursor: pointer'>" +
"<span class='panel-title text-center'>控制台</span> " +
"<span class='glyphicon glyphicon-triangle-top' id='toggleIcon' ></span></div>").appendTo(me._div);
var contentDiv = $("<div class='panel-body content center-block' style='font-size: 14px'></div>").appendTo(me._div);
var optionsDiv = $("<div class='' id='options'></div>").appendTo(contentDiv);
$("<div class='form-group'><label class='text-right' for='flightIds' >" +
"航班号<span style='color:red'>*</span></label>" +
"<select class='selectpicker ' id='flightIds' multiple data-actions-box='true' title='请选择'>" +
initSelectOpitons() +
"</select></div><hr/>").appendTo(optionsDiv);
function initSelectOpitons() {
var str = '';
flightIdens.map(function (ident, key) {
if (key !== 0) {
str += "<option value=" + ident + ">" + ident + "</option> "
} else {
str += "<option value=" + ident + " selected>" + ident + "</option> "
}
});
return str;
}
var defaultOption = getDefaultControlOptions();
var defaultGeoFence = getDefaultGeoFence();
$("<div class='form-group form-inline'><label class='text-right' for='geoFence' >地理围栏</label>" +
"<input id='geoFence' type='text' class='form-control input-sm' placeholder='" + JSON.stringify(defaultGeoFence) +
"' value='" + JSON.stringify(defaultGeoFence) + "'/></div></div><hr/>").appendTo(optionsDiv);
$("<div class='form-group form-inline'><label class='text-right' for='startTime' >起始时间<span style='color:red'>*</span></label>" +
"<input id='startTime' type='text' class='form-control input-sm' placeholder='" + defaultOption.startTime +
"' value='" + defaultOption.startTime + "'/></div></div>").appendTo(optionsDiv);
$("<div class='form-group form-inline'><label class='text-right' for='endTime' >终止时间<span style='color:red'>*</span></label>" +
"<input id='endTime' type='text' class='form-control input-sm' placeholder='" + defaultOption.endTime +
"' value='" + defaultOption.endTime + "'/></div></div>").appendTo(optionsDiv);
$("<div class='form-group form-inline'><label class='text-right' for='speed' >刷新步长(ms)</label>" +
"<input id='speed' type='number' min='1' class='form-control input-sm' placeholder='" + defaultOption.speed +
"' value='" + defaultOption.speed + "'/></div></div>").appendTo(optionsDiv);
$("<div class='form-group form-inline'><label class='text-right' for='frequency' >刷新频率(ms)</label>" +
"<input id='frequency' type='number' min='1' class='form-control input-sm' placeholder='" + defaultOption.frequency +
"' value='" + defaultOption.frequency + "'/></div></div>").appendTo(optionsDiv);
var progressDiv = $("<div class='form-group'><div class='form-horizontal text-center'><div class='form-group'>" +
"<label for='progress'>当前时间:</label><span class='form-control-static' id='progress'>未开始</span>" +
"</div></div></div>").appendTo(contentDiv);
var controlDiv = $("<section><div class='form-inline text-center'>" +
"<input id='start' type='button' class='btn btn-default text-center' value='开始'/> " +
"<input id='pause' type='button' class='btn btn-default text-center' value='暂停'/> " +
"<input id='stop' type='button' class='btn btn-default text-center' value='停止'/>" +
"</div></section>").appendTo(contentDiv);
me._div.addEventListener('mouseover', function () {
me._map.dragging.disable();
me._map.scrollWheelZoom.disable();
me._map.doubleClickZoom.disable();
});
me._div.addEventListener('mouseout', function () {
me._map.dragging.enable();
me._map.scrollWheelZoom.enable();
me._map.doubleClickZoom.enable();
});
return me._div;
};
control.addTo(map);
var dateOptions = {
format: "YYYY-MM-DD HH:mm:ss",
stepping: 1,
showClose: true,
locale: 'zh-cn'
};
$("#startTime").datetimepicker(dateOptions);
$("#endTime").datetimepicker(dateOptions);
$('#flightIds').selectpicker();
$("#start").on('click', function () {
$("#options").slideUp("fast", function () {
toggle(this);
});
start();
});
$("#pause").on('click', pause);
$("#stop").on('click', stop);
$("#toggle").on('click', function () {
$("#options").slideToggle("fast", function () {
toggle(this);
});
return false;
});
function toggle(ele) {
if ($(ele).is(":visible")) {
$("#toggleIcon").attr('class', "glyphicon glyphicon-triangle-top");
} else {
$("#toggleIcon").attr('class', "glyphicon glyphicon-triangle-bottom");
}
}
}
function clearAll() {
if (timeControl) {
timeControl.destroy();
timeControl = null;
}
if (trackRenderer) {
map.removeLayer(trackRenderer);
trackRenderer = null;
}
if (liveRenderer) {
map.removeLayer(liveRenderer);
liveRenderer = null;
}
if (liveDataSet) {
liveDataSet = null;
}
if (planeLabels) {
for (var item in planeLabels) {
planeLabels[item].remove();
}
planeLabels = {};
}
if (geoFenceLayer) {
geoFenceLayer.remove();
geoFenceLayer = null;
}
}
</script>
</body>
</html>