/
geojson_anime_button.html
380 lines (356 loc) · 14.5 KB
/
geojson_anime_button.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0" />
<title>Drag and Drop GeoJSON</title>
<style>
html { height: 100% }
body { height: 100%; margin: 0; padding: 0; overflow: hidden; }
#map { height: 100% }
#drop-container {
display: none;
height: 100%;
width: 100%;
position: absolute;
z-index: 1;
top: 0px;
left: 0px;
padding: 20px;
background-color: rgba(100, 100, 100, 0.5);
}
#drop-silhouette {
color: white;
border: white dashed 8px;
height: calc(100% - 56px);
width: calc(100% - 56px);
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IArs4c6QAAAAZiS0dEAGQAZABkkPCsTwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB90LHAIvICWdsKwAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAACdklEQVR42u3csU7icBzA8Xp3GBMSeRITH8JHMY7cRMvmVmXoE9TAcJubhjD4ApoiopgqDMWAKAgIcSAiCfxuwhwROVJbkPD9rP23ob8vpZCQKgoAAAAAAAAAAPDYyiK/eNM05bNtr6+vSjgcXiHxDMkE1WpVFvGcfpCVICAIQUAQgoAgBAFBCAKCgCAEAUEIAoIQBAQhCAgCghAEBCEICEIQEIQgIAgIQhAQhCAgCEFAEIKAICAIQUAQgoAgBAFBCDIzhmFINBo9/K6D0XVddnd3ZaneDY7jSCqVcn3SfjyeKRKJbJ2dnYllWbKUl2i5XJaXlxdJJBIy7yDHx8fy9vYm6XR6OWMM3d/fi4hIqVSSWCwmsw5ycHAgrVZLRETOz8+XO8ZQpVJ5H2Y6nRZN0/b9DqLruhSLxfd9MpkMMT6L0uv1JJlMih9BhveJwWDwvv7i4oIY4zw8PIwMtt1uSzweF6+CHB0dSbfbHVmbzWaJMcnj4+OHAd/d3cne3p64DWKapjw/P39Yd3l5SYxpVKvVsYO2LEtUVd2ZNoiu6+I4ztg1V1dXxPAiSq/Xk5OTk0k9pNVqyenp6ch94l+5XI4YbtRqNfHa9fX1t43xcwGa/Nnc3PwdDAY9OZht28rGxgZPvP6KSCSy9fT09OUrw7ZtPqa8jFKv113HuLm5IYbXVFXdcRPl9vaWGH5GaTQaU8fI5/PE8JumafvNZvO/MQqFAjFmJRqNHk6Ksqgx5vr1zzAM2d7edr3/6uqqsra2NnZbp9NR+v2+62OHQqG5zObXPIMEAgFlfX3dl2N79btl1viTA0FAEIKAIAQBAAAAAAAAsMz+Ai1bUgo6ebm8AAAAAElFTkSuQmCC');
background-repeat: no-repeat;
background-position: center;
}
#floating-panel {
position: absolute;
top: 10px;
left: 15%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="floating-panel" style="display: none;">
<input id="start_time" type="datetime-local" name="period" step="1">
<input id="end_time" type="datetime-local" name="period" step="1">
<input onclick="showMarkers();" type=button value="Show Markers">
<input onclick="clearMarkers();" type=button value="Hide Markers">
<input onclick="deleteMarkers(true);" type=button value="Delete Markers">
<input onclick="downloadGeoJson();" type=button value="Download GeoJSON">
</div>
<div id="map"></div>
<div id="drop-container"><div id="drop-silhouette"></div></div>
<script>
/* Map functions */
var map;
var timer = null;
var line = null;
function geoPoint(marker, feature) {
this.marker = marker;
this.feature = feature;
this.timestamp = convetStringToDate(feature.properties.time);
this.event = (feature.properties.event != null)?feature.properties.event:"NULL";
this.content = (feature.properties.content != null)?feature.properties.content:"NULL";
};
var geoPoints = [];
function initMap() {
// set up the map
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 35.681084, lng: 139.767617},
zoom: 15
});
}
function createGoogleMapsUrl(lat, lng){
var url = '<a href="https://www.google.com/maps/search/?api=1&query=' + lat + ',' + lng + '" target="_blank">' + lat + ',' + lng + '</a>';
return url;
}
function addMarker(feature) {
var coords = feature.geometry.coordinates;
var marker = new google.maps.Marker({
position: new google.maps.LatLng(coords[1],coords[0]),
visible: (feature.properties.event != null)?((feature.properties.event != "NULL")?true:false):false,
label: (feature.properties.event != null)?((feature.properties.event != "NULL")?feature.properties.event[0]:null):null,
map: map
});
geoPoints.push(new geoPoint(marker, feature));
marker.addListener('click', function(event) {
var infoWindow = new google.maps.InfoWindow;
var pos = {
lat: event.latLng.lat(),
lng: event.latLng.lng()
};
var content = feature.properties.time + '<br>' + createGoogleMapsUrl(pos.lat.toFixed(6), pos.lng.toFixed(6));
if (feature.properties.content != "NULL"){
content += '<br>'
content += feature.properties.content
}
infoWindow.setPosition(pos);
infoWindow.setContent(content);
infoWindow.open(map, marker);
});
}
function sortMarkers(){
// Sort Geo Data by timestamp
geoPoints.sort(function(a,b){
return a.timestamp - b.timestamp;
});
for (var i = 0; i < geoPoints.length; i++) {
if(geoPoints[i].marker.label !== null){
geoPoints[i].marker.visible = true;
geoPoints[i].marker.setMap(map);
}
}
// Set the first geo data as start data
if(geoPoints[0].marker.label === null){
geoPoints[0].marker.label = "S";
geoPoints[0].marker.visible = true;
geoPoints[0].marker.setMap(map);
}
// Set the last geo data as end data
if(geoPoints[i-1].marker.label === null){
geoPoints[i-1].marker.label = "E";
geoPoints[i-1].marker.visible = true;
geoPoints[i-1].marker.setMap(map);
}
}
function loadGeoJsonString(geoString) {
// turn off all markers and line
deleteMarkers(false);
var geojson = JSON.parse(geoString);
for (var i = 0; i < geojson.features.length; i++) {
var feature = geojson.features[i];
//Only Point data should be added as Markers
if (feature.geometry.type == "Point"){
addMarker(feature);
}
}
sortMarkers();
//Update the center of the map
map.setCenter(geoPoints[0].marker.position);
//Set start time and end time
setTimePanelVisible(geoPoints[0].timestamp, geoPoints[geoPoints.length - 1].timestamp);
//Draw a line by Point data
setLine(map);
}
//Set start time and end time on floating-panel
//Also set display value as block to show floating-panel
function setTimePanelVisible(time_start, time_max){
var time_min = new Date(time_start);
var time_end = new Date(time_max);
time_min.setSeconds(time_start.getSeconds() + 1);
time_end.setSeconds(time_end.getSeconds() + 1);
document.getElementById("start_time").value = convetDateToIso(time_start);
document.getElementById("start_time").min = convetDateToIso(time_start);
document.getElementById("start_time").max = convetDateToIso(time_max);
document.getElementById("end_time").min = convetDateToIso(time_min)
document.getElementById("end_time").max = convetDateToIso(time_end);
document.getElementById("end_time").value = convetDateToIso(time_end);
document.getElementById('floating-panel').style.display = 'block';
}
/* DOM (drag/drop) functions */
function initEvents() {
// set up the drag & drop events
var mapContainer = document.getElementById('map');
var dropContainer = document.getElementById('drop-container');
// map-specific events
mapContainer.addEventListener('dragenter', showPanel, false);
// overlay specific events (since it only appears once drag starts)
dropContainer.addEventListener('dragover', showPanel, false);
dropContainer.addEventListener('drop', handleDrop, false);
dropContainer.addEventListener('dragleave', hidePanel, false);
}
function showPanel(e) {
e.stopPropagation();
e.preventDefault();
document.getElementById('drop-container').style.display = 'block';
return false;
}
function hidePanel(e) {
document.getElementById('drop-container').style.display = 'none';
}
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
hidePanel(e);
var files = e.dataTransfer.files;
if (files.length) {
// process file(s) being dropped
// grab the file data from each file
for (var i = 0, file; file = files[i]; i++) {
var reader = new FileReader();
reader.onload = function(e) {
loadGeoJsonString(e.target.result);
};
reader.onerror = function(e) {
console.error('reading failed');
};
reader.readAsText(file);
}
} else {
// process non-file (e.g. text or html) content being dropped
// grab the plain text version of the data
var plainText = e.dataTransfer.getData('text/plain');
if (plainText) {
loadGeoJsonString(plainText);
}
}
// prevent drag event from bubbling further
return false;
}
function setLine(map){
// Define the symbol, using one of the predefined paths ('CIRCLE')
// supplied by the Google Maps JavaScript API.
var lineSymbol = {
path: google.maps.SymbolPath.CIRCLE,
scale: 5,
strokeColor: '#393'
};
// Create the polyline and add the symbol to it via the 'icons' property.
line = new google.maps.Polyline({
icons: [{
icon: lineSymbol,
offset: '100%'
}],
map: map
});
var path = line.getPath();
for(var i = 0; i < geoPoints.length; i++){
if(geoPoints[i].event != "Waypoint"){
var latlng = geoPoints[i].marker.position;
path.push(latlng);
}
}
animateCircle(line);
}
// Use the DOM setInterval() function to change the offset of the symbol
// at fixed intervals.
function animateCircle(line) {
var count = 0;
timer = window.setInterval(function() {
count = (count + 1) % 200;
var icons = line.get('icons');
icons[0].offset = (count / 2) + '%';
line.set('icons', icons);
}, 20);
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
var s = new Date(document.getElementById("start_time").value);
var e = new Date(document.getElementById("end_time").value);
for (var i = 0; i < geoPoints.length; i++) {
if(geoPoints[i].marker.label === null && (geoPoints[i].timestamp >= s && geoPoints[i].timestamp <= e)){
geoPoints[i].marker.visible = false;
geoPoints[i].marker.setMap(null);
}
else {
geoPoints[i].marker.visible = true;
geoPoints[i].marker.setMap(map);
}
}
}
// Shows any markers currently in the array.
function showMarkers() {
var s = new Date(document.getElementById("start_time").value);
var e = new Date(document.getElementById("end_time").value);
for (var i = 0; i < geoPoints.length; i++) {
if(geoPoints[i].marker.label !== null || (geoPoints[i].timestamp >= s && geoPoints[i].timestamp <= e)){
geoPoints[i].marker.visible = true;
geoPoints[i].marker.setMap(map);
}
else {
geoPoints[i].marker.visible = false;
geoPoints[i].marker.setMap(null);
}
}
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers(all) {
for (var i = 0; i < geoPoints.length; i++) {
if(geoPoints[i].marker.label === "S" || geoPoints[i].marker.label === "E"){
geoPoints[i].marker.label = null;
}
geoPoints[i].marker.visible = false;
geoPoints[i].marker.setMap(null);
}
window.clearInterval(timer);
document.getElementById('floating-panel').style.display = 'none';
if(line != null){
line.setMap(null);
delete line;
line = null;
}
if(all === true){
geoPoints = [];
}
}
/* convert evet time to datetime-local format
in: yyyy/mm/dd HH:MM:SS
return: Date object
*/
function convetStringToDate(s){
var year = s.substr(0,4);
var month = s.substr(5,2);
var day = s.substr(8,2);
var hour = s.substr(11,2);
var minutes = s.substr(14,2);
var second = s.substr(17,2);
var d = new Date(year, month-1, day, hour, minutes, second);
return d;
}
/* convert evet time to datetime-local format
in: Date object
retrun : yyyy-mm-ddTHH:MM:SS
*/
function convetDateToIso(d){
return d.getFullYear()+'-'
+ (("0"+(d.getMonth() + 1)).slice(-2))+'-'
+ ("0"+d.getDate()).slice(-2)+'T'
+ ("0"+d.getHours()).slice(-2)+':'
+ ("0"+d.getMinutes()).slice(-2)+':'
+ ("0"+d.getSeconds()).slice(-2);
}
function convetDateToStr(d){
return d.getFullYear()
+ (("0"+(d.getMonth() + 1)).slice(-2))
+ ("0"+d.getDate()).slice(-2)+'_'
+ ("0"+d.getHours()).slice(-2)
+ ("0"+d.getMinutes()).slice(-2)
+ ("0"+d.getSeconds()).slice(-2);
}
function downloadGeoJson() {
var features = [];
for(var i = 0; i < geoPoints.length; i++){
var feature = geoPoints[i].feature;
feature.id = i;
features.push(feature);
}
var data = JSON.stringify({"type": "FeatureCollection", "features": features}, null, 2);
var link = document.createElement("a");
link.download = convetDateToStr(geoPoints[0].timestamp) + "-" + convetDateToStr(geoPoints[geoPoints.length-1].timestamp) + ".geojson"
link.href = URL.createObjectURL(new Blob([data], {type: "text.plain"}));
link.dataset.downloadurl = ["text/plain", link.download, link.href].join(":");
link.click();
}
function initialize() {
initMap();
initEvents();
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=initialize"></script>
</body>
</html>