-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
659 lines (582 loc) · 19.9 KB
/
index.js
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import 'ol/ol.css';
import Circle from 'ol/geom/Circle';
import Feature from 'ol/Feature';
import GeoJSON from 'ol/format/GeoJSON';
import LineString from 'ol/geom/LineString';
import Map from 'ol/Map';
import View from 'ol/View';
import ol from 'ol';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {transform} from 'ol/proj';
import { getVectorContext } from 'ol/render';
import * as turf from '@turf/turf';
import Point from 'ol/geom/Point';
import {toLonLat} from 'ol/proj';
import {toStringHDMS} from 'ol/coordinate';
import {altKeyOnly, click, pointerMove} from 'ol/events/condition';
import {
defaults as defaultInteractions,
Select
} from "ol/interaction";
import Overlay from 'ol/Overlay';
var container = document.getElementById('popup');
var content_element = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
/**
* Create an overlay to anchor the popup to the map.
*/
var overlay = new Overlay({
element: container,
autoPan: true,
offset: [-20,30],
});
var tileLayer = new TileLayer({
preload: 4,
source: new OSM(),
});
map.addOverlay(overlay);
map.addLayer(tileLayer);
// /**
// * Add a click handler to hide the popup.
// * @return {boolean} Don't follow the href.
// */
closer.onclick = function () {
overlay.setPosition(undefined);
closer.blur();
return false;
};
//修改选择功能的默认属性
var selectPointerMove = new Select({
condition: pointerMove,
});
// //总网络link的style
// var styles = {
// 'LineString': new Style({
// stroke: new Stroke({
// color: 'rgba(200,200,200)',
// width: 3.5,
// }),
// }),
// };
// var styleFunction = function (feature) {
// return styles[feature.getGeometry().getType()];
// };
// //总Link的数据
// var link_data = window.link_geojson_data;
// //转化geojson数据的坐标系
// for(var i = 0;i < link_data.features.length;i++){
// //开始转换
// link_data.features[i].geometry.coordinates[0] = transform(link_data.features[i].geometry.coordinates[0],'EPSG:4326','EPSG:3857');
// link_data.features[i].geometry.coordinates[1] = transform(link_data.features[i].geometry.coordinates[1],'EPSG:4326','EPSG:3857');
// };
// //总网络geo数据
// var geojsonObject = link_data;
// //加载道路geojson对象数据
// var vectorSource = new VectorSource({
// features: new GeoJSON().readFeatures(geojsonObject),
// });
// var link_Layer = new VectorLayer({
// source: vectorSource,
// style: styleFunction,
// visible: true,
// });
var iiii = transform([-76.898229,38.999111],'EPSG:4326','EPSG:3857');
console.log(iiii);
//绑定多选框
let controls = document.getElementById('controls');
// 事件委托
controls.addEventListener('click', (event) => {
if(event.target.checked){ // 如果选中某一复选框
// 通过DOM元素的id值来判断应该对哪个图层进行显示
switch(event.target.id){
case "Agent1":
map.getLayers().item(1).setVisible(true);
break;
case "Agent2":
map.getLayers().item(2).setVisible(true);
break;
case "Agent3":
map.getLayers().item(3).setVisible(true);
break;
case "Agent4":
map.getLayers().item(4).setVisible(true);
break;
case "Agent5":
map.getLayers().item(5).setVisible(true);
break;
case "Node":
map.getLayers().item(6).setVisible(true);
break;
}
}else{ // 如果取消某一复选框
// 通过DOM元素的id值来判断应该对哪个图层进行隐藏
switch(event.target.id){
case "Agent1":
map.getLayers().item(1).setVisible(false);
break;
case "Agent2":
map.getLayers().item(2).setVisible(false);
break;
case "Agent3":
map.getLayers().item(3).setVisible(false);
break;
case "Agent4":
map.getLayers().item(4).setVisible(false);
break;
case "Agent5":
map.getLayers().item(5).setVisible(false);
break;
case "Node":
map.getLayers().item(6).setVisible(false);
break;
}
}
});
map.on('click', function (evt) {
var feature = map.forEachFeatureAtPixel(evt.pixel,function(feature, layer)
{
return feature;
});
if (feature) {
var geometry = feature.getGeometry();
var coord = evt.coordinate
var contents = '<p>title_name1: content</p>'+'<br/>'+'<p>title_name2: content</p>'+'<br/>'+'<p>......</p>';
content_element.innerHTML = contents;
overlay.setPosition(coord);
}
});
map.on('pointermove', function(e) {
if (e.dragging) return;
var pixel = map.getEventPixel(e.originalEvent);
var hit = map.hasFeatureAtPixel(pixel);
map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
//动态路径1的样式
var style_1 = new Style({
stroke: new Stroke({
color: 'rgba(27,75,153)',
width: 4,
}),
});
//动态路径2的样式
var style_2 = new Style({
stroke: new Stroke({
color: 'rgba(210,46,41)',
width: 4,
}),
});
//动态路径3的样式
var style_3 = new Style({
stroke: new Stroke({
color: 'rgba(210,46,41)',
width: 4,
}),
});
//动态路径4的样式
var style_4 = new Style({
stroke: new Stroke({
color: 'rgba(27,75,153)',
width: 4,
}),
});
//动态路径5的样式
var style_5 = new Style({
stroke: new Stroke({
color: 'rgba(154,192,67)',
width: 4,
}),
});
//用来存储所有的feature的坐标
var sourceString_1 = [];
//动态路径1的source
var flightsSource = new VectorSource({
wrapX: false,
attributions:
'Link and node data by ' +
'<a href="http://openflights.org/data.html">Xuesong Zhou</a>,',
loader: function(){
var flightsData = window.flights1;
for (var i = 0; i < flightsData.length; i++) {
var flight = flightsData[i];
var from = flight[0];
var to = flight[1];
// create an arc circle between the two locations
var arcGenerator = new arc.GreatCircle(
{x: from[0], y: from[1]},
{x: to[0], y: to[1]}
);
var arcLine = arcGenerator.Arc(100, {offset: 0});
sourceString_1 = sourceString_1.concat(arcLine.geometries[0].coords);
}
// var line = new LineString(arcLine.geometries[0].coords);
var line = new LineString(sourceString_1);
line.transform('EPSG:4326', 'EPSG:3857');
var feature = new Feature({
geometry: line,
finished: false,
});
// add the feature with a delay so that the animation
// for all features does not start at the same time
addLater(feature, 0);
tileLayer.on('postrender', animateFlights);
}
});
//用来存储所有的feature的坐标
var sourceString_2 = [];
//动态路径1的source
var flightsSource_2 = new VectorSource({
wrapX: false,
attributions:
'Link and node data by ' +
'<a href="http://openflights.org/data.html">Xuesong Zhou</a>,',
loader: function(){
var flightsData = window.flights_2;
for (var i = 0; i < flightsData.length; i++) {
var flight = flightsData[i];
var from = flight[0];
var to = flight[1];
// create an arc circle between the two locations
var arcGenerator = new arc.GreatCircle(
{x: from[0], y: from[1]},
{x: to[0], y: to[1]}
);
var arcLine = arcGenerator.Arc(100, {offset: 0});
sourceString_2 = sourceString_2.concat(arcLine.geometries[0].coords);
}
// var line = new LineString(arcLine.geometries[0].coords);
var line = new LineString(sourceString_2);
line.transform('EPSG:4326', 'EPSG:3857');
var feature = new Feature({
geometry: line,
finished: false,
});
// add the feature with a delay so that the animation
// for all features does not start at the same time
addLater_2(feature, 0);
tileLayer.on('postrender', animateFlights_2);
}
});
//用来存储所有的feature的坐标
var sourceString_3 = [];
//动态路径3的source
var flightsSource_3 = new VectorSource({
wrapX: false,
attributions:
'Link and node data by ' +
'<a href="http://openflights.org/data.html">Xuesong Zhou</a>,',
loader: function(){
var flightsData = window.flights_3;
for (var i = 0; i < flightsData.length; i++) {
var flight = flightsData[i];
var from = flight[0];
var to = flight[1];
// create an arc circle between the two locations
var arcGenerator = new arc.GreatCircle(
{x: from[0], y: from[1]},
{x: to[0], y: to[1]}
);
var arcLine = arcGenerator.Arc(100, {offset: 0});
sourceString_3 = sourceString_3.concat(arcLine.geometries[0].coords);
}
// var line = new LineString(arcLine.geometries[0].coords);
var line = new LineString(sourceString_3);
line.transform('EPSG:4326', 'EPSG:3857');
var feature = new Feature({
geometry: line,
finished: false,
});
// add the feature with a delay so that the animation
// for all features does not start at the same time
addLater_3(feature, 0);
tileLayer.on('postrender', animateFlights_3);
}
});
//用来存储所有的feature的坐标
var sourceString_4 = [];
//动态路径3的source
var flightsSource_4 = new VectorSource({
wrapX: false,
attributions:
'Link and node data by ' +
'<a href="http://openflights.org/data.html">Xuesong Zhou</a>,',
loader: function(){
var flightsData = window.flights_4;
for (var i = 0; i < flightsData.length; i++) {
var flight = flightsData[i];
var from = flight[0];
var to = flight[1];
// create an arc circle between the two locations
var arcGenerator = new arc.GreatCircle(
{x: from[0], y: from[1]},
{x: to[0], y: to[1]}
);
var arcLine = arcGenerator.Arc(100, {offset: 0});
sourceString_4 = sourceString_4.concat(arcLine.geometries[0].coords);
}
// var line = new LineString(arcLine.geometries[0].coords);
var line = new LineString(sourceString_4);
line.transform('EPSG:4326', 'EPSG:3857');
var feature = new Feature({
geometry: line,
finished: false,
});
// add the feature with a delay so that the animation
// for all features does not start at the same time
addLater_4(feature, 0);
tileLayer.on('postrender', animateFlights_4);
}
});
//用来存储所有的feature的坐标
var sourceString_5 = [];
//动态路径3的source
var flightsSource_5 = new VectorSource({
wrapX: false,
attributions:
'Link and node data by ' +
'<a href="http://openflights.org/data.html">Xuesong Zhou</a>,',
loader: function(){
var flightsData = window.flights_5;
for (var i = 0; i < flightsData.length; i++) {
var flight = flightsData[i];
var from = flight[0];
var to = flight[1];
// create an arc circle between the two locations
var arcGenerator = new arc.GreatCircle(
{x: from[0], y: from[1]},
{x: to[0], y: to[1]}
);
var arcLine = arcGenerator.Arc(100, {offset: 0});
sourceString_5 = sourceString_5.concat(arcLine.geometries[0].coords);
}
// var line = new LineString(arcLine.geometries[0].coords);
var line = new LineString(sourceString_5);
line.transform('EPSG:4326', 'EPSG:3857');
var feature = new Feature({
geometry: line,
finished: false,
});
// add the feature with a delay so that the animation
// for all features does not start at the same time
addLater_5(feature, 0);
tileLayer.on('postrender', animateFlights_5);
}
});
var flightsLayer_1 = new VectorLayer({
source: flightsSource,
style: function (feature) {
// if the animation is still active for a feature, do not
// render the feature with the layer style
if (feature.get('finished')) {
return style_1;
} else {
return null;
}
},
});
var flightsLayer_2 = new VectorLayer({
source: flightsSource_2,
style: function (feature) {
// if the animation is still active for a feature, do not
// render the feature with the layer style
if (feature.get('finished')) {
return style_2;
} else {
return null;
}
},
});
var flightsLayer_3 = new VectorLayer({
source: flightsSource_3,
style: function (feature) {
// if the animation is still active for a feature, do not
// render the feature with the layer style
if (feature.get('finished')) {
return style_3;
} else {
return null;
}
},
});
var flightsLayer_4 = new VectorLayer({
source: flightsSource_4,
style: function (feature) {
// if the animation is still active for a feature, do not
// render the feature with the layer style
if (feature.get('finished')) {
return style_4;
} else {
return null;
}
},
});
var flightsLayer_5 = new VectorLayer({
source: flightsSource_5,
style: function (feature) {
// if the animation is still active for a feature, do not
// render the feature with the layer style
if (feature.get('finished')) {
return style_5;
} else {
return null;
}
},
});
// map.addLayer(link_Layer);
map.addLayer(flightsLayer_1);
map.addLayer(flightsLayer_2);
map.addLayer(flightsLayer_3);
map.addLayer(flightsLayer_4);
map.addLayer(flightsLayer_5);
var pointsPerMs = 0.2;
function animateFlights(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
vectorContext.setStyle(style_1);
var features = flightsSource.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if (!feature.get('finished')) {
// only draw the lines for which the animation has not finished yet
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new LineString(coords.slice(0, maxIndex));
// directly draw the line with the vector context
vectorContext.drawGeometry(currentLine);
}
}
// tell OpenLayers to continue the animation
map.render();
}
function animateFlights_2(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
vectorContext.setStyle(style_2);
var features = flightsSource_2.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if (!feature.get('finished')) {
// only draw the lines for which the animation has not finished yet
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new LineString(coords.slice(0, maxIndex));
// directly draw the line with the vector context
vectorContext.drawGeometry(currentLine);
}
}
// tell OpenLayers to continue the animation
map.render();
}
function animateFlights_3(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
vectorContext.setStyle(style_3);
var features = flightsSource_3.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if (!feature.get('finished')) {
// only draw the lines for which the animation has not finished yet
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new LineString(coords.slice(0, maxIndex));
// directly draw the line with the vector context
vectorContext.drawGeometry(currentLine);
}
}
// tell OpenLayers to continue the animation
map.render();
}
function animateFlights_4(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
vectorContext.setStyle(style_4);
var features = flightsSource_4.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if (!feature.get('finished')) {
// only draw the lines for which the animation has not finished yet
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new LineString(coords.slice(0, maxIndex));
// directly draw the line with the vector context
vectorContext.drawGeometry(currentLine);
}
}
// tell OpenLayers to continue the animation
map.render();
}
function animateFlights_5(event) {
var vectorContext = getVectorContext(event);
var frameState = event.frameState;
vectorContext.setStyle(style_5);
var features = flightsSource_5.getFeatures();
for (var i = 0; i < features.length; i++) {
var feature = features[i];
if (!feature.get('finished')) {
// only draw the lines for which the animation has not finished yet
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new LineString(coords.slice(0, maxIndex));
// directly draw the line with the vector context
vectorContext.drawGeometry(currentLine);
}
}
// tell OpenLayers to continue the animation
map.render();
}
function addLater(feature, timeout) {
window.setTimeout(function () {
feature.set('start', new Date().getTime());
flightsSource.addFeature(feature);
}, timeout);
}
function addLater_2(feature, timeout) {
window.setTimeout(function () {
feature.set('start', new Date().getTime());
flightsSource_2.addFeature(feature);
}, timeout);
}
function addLater_3(feature, timeout) {
window.setTimeout(function () {
feature.set('start', new Date().getTime());
flightsSource_3.addFeature(feature);
}, timeout);
}
function addLater_4(feature, timeout) {
window.setTimeout(function () {
feature.set('start', new Date().getTime());
flightsSource_4.addFeature(feature);
}, timeout);
}
function addLater_5(feature, timeout) {
window.setTimeout(function () {
feature.set('start', new Date().getTime());
flightsSource_5.addFeature(feature);
}, timeout);
}