-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanimate_tracks.js
145 lines (124 loc) · 4.54 KB
/
animate_tracks.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
// Global variables to track play/pause state and the interval ID
let isPlaying = false;
let playbackInterval;
document.addEventListener('DOMContentLoaded', () => {
const slider = createSlider();
const timeLegend = createTimeLegend(slider);
document.body.appendChild(slider);
document.body.appendChild(timeLegend);
// Access the Folium-initialized map using the dynamically generated map_id
const map = window[map_id];
const trackMarkers = initializeTrackMarkers(map);
slider.addEventListener('input', () => {
updateTrackMarkers(slider, trackMarkers);
updateTimeDisplay(slider, timeLegend);
});
// Initialize with the first timestamp
slider.dispatchEvent(new Event('input'));
});
function createSlider() {
const slider = document.createElement('input');
slider.type = 'range';
slider.min = 0;
slider.max = 1000;
slider.value = 0;
Object.assign(slider.style, {
position: 'fixed',
bottom: '20px',
left: '50%',
transform: 'translateX(-50%)',
width: '80%',
zIndex: 1000,
});
return slider;
}
function createTimeLegend(slider) {
const timeLegend = document.createElement('div');
Object.assign(timeLegend.style, {
position: 'fixed',
bottom: '50px',
right: '10px',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
padding: '5px',
border: '1px solid white',
borderRadius: '5px',
zIndex: 1000,
color: 'white',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
});
const timeDisplay = document.createElement('div');
timeDisplay.style.marginBottom = '2px';
timeLegend.appendChild(timeDisplay);
const playPauseButton = document.createElement('button');
playPauseButton.innerHTML = '⏯️'; // Play/Pause icon
playPauseButton.style.marginTop = '5px';
playPauseButton.addEventListener('click', () => togglePlayPause(slider, playPauseButton));
timeLegend.appendChild(playPauseButton);
timeLegend.timeDisplay = timeDisplay;
return timeLegend;
}
function initializeTrackMarkers(map) {
const colors = ['red', 'green', 'blue', 'orange', 'purple', 'brown', 'pink', 'yellow', 'cyan', 'magenta'];
return gpx_points_data.map((track, index) => {
const color = colors[index % colors.length];
const marker = L.circleMarker([track[0].lat, track[0].lon], {
radius: 4,
color: color,
fillColor: color,
fillOpacity: 0.5
}).addTo(map);
return marker;
});
}
function updateTrackMarkers(slider, trackMarkers) {
const timeIndex = Math.floor(slider.value / 1000 * (gpx_timestamps.length - 1));
const currentTime = new Date(gpx_timestamps[timeIndex]).getTime();
trackMarkers.forEach((marker, trackIndex) => {
const track = gpx_points_data[trackIndex];
let closestPoint = track[0];
for (let i = 1; i < track.length; i++) {
const pointTime = new Date(track[i].time).getTime();
if (pointTime <= currentTime) {
closestPoint = track[i];
} else {
break;
}
}
marker.setLatLng([closestPoint.lat, closestPoint.lon]);
});
}
function updateTimeDisplay(slider, timeLegend) {
const timeIndex = Math.floor(slider.value / 1000 * (gpx_timestamps.length - 1));
const currentTime = new Date(gpx_timestamps[timeIndex]).getTime();
timeLegend.timeDisplay.innerHTML = `${new Date(currentTime).toUTCString()}`;
}
function updateSlider(slider) {
if (parseInt(slider.value) < parseInt(slider.max)) {
slider.value = parseInt(slider.value) + 1;
slider.dispatchEvent(new Event('input'));
} else {
// Slider has reached the maximum value
clearInterval(playbackInterval);
isPlaying = false;
resetPlayPauseButton();
}
}
function togglePlayPause(slider, playPauseButton) {
if (isPlaying) {
clearInterval(playbackInterval);
resetPlayPauseButton();
isPlaying = false; // Ensure isPlaying is set to false when paused
} else {
playbackInterval = setInterval(() => updateSlider(slider), 100);
playPauseButton.style.backgroundColor = 'gray';
playPauseButton.innerHTML = '⏸️';
isPlaying = true; // Set isPlaying to true when playback starts
}
}
function resetPlayPauseButton() {
const playPauseButton = document.querySelector('button');
playPauseButton.style.backgroundColor = '';
playPauseButton.innerHTML = '⏯️';
}