-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll.html
460 lines (449 loc) · 14 KB
/
ll.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
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
<!DOCTYPE html>
<!--
This program records a device's location with human-readable lines
Normally the file should be hosted in https://vps.hcc.io/l.html
Copyright (C) 2020-2022 Eric Li 李宵逸
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0;
maximum-scale=1.0; user-scalable=no">
<title>LocationTrackerWeb</title>
<style>
* {
box-sizing: border-box;
}
html, body {
height: 100%;
}
body {
margin: 0px;
padding: 8px;
overflow: hidden; /* disable scroll */
}
body.flex {
display: flex;
flex-direction: column;
}
div.page {
flex-grow: 1;
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.col-2 {
flex-grow: 1;
float: left;
overflow: hidden;
}
.col-4 {
flex-grow: 2;
float: left;
overflow: hidden;
}
textarea {
width: 100%;
flex-grow: 1;
white-space: pre;
overflow-wrap: normal;
overflow-x: scroll;
}
.big {
font-size: 1.5em;
}
.gray {
color: gray;
}
span.border {
border: 1px solid gray;
/* white-space: nowrap; */
}
</style>
</head>
<body class="flex">
<div id="upper_btns" class="row">
<button class="col-2" onclick="watchLocation();">追踪</button>
<button class="col-2" onclick="unwatchLocation();">停止</button>
<button class="col-2" onclick="toggleConfig();">设置</button>
<button class="col-2" onclick="answer_scroll();">滚动</button>
<button class="col-4" onclick="swap_info();">交换</button>
</div>
<div id="main_page" class="page">
<div id="info_table">
<div class="row big">
<div class="col-4">
<div class="row"><span class="col-4 border" id="lo">经度</span></div>
<div class="row"><span class="col-4 border" id="la">纬度</span></div>
</div>
<div class="col-2">
<div class="row">
<span class="col-2 border"><span id="al">高度米</span> m</span>
</div>
<div class="row">
<span class="col-2 border"><span id="ft">高度英尺</span> ft</span>
</div>
</div>
</div>
<div class="row">
<span class="col-4 border big"><span id="he">朝向</span> deg</span>
<span class="col-2 border gray"><span id="ac">精确度</span> m</span>
<span class="col-4 border big"><span id="sp">速度</span> m/s</span>
<span class="col-2 border gray"><span id="aa">高度精确度</span> m</span>
</div>
<div class="row big">
<span class="col-4 border"><span id="kts">速度节</span> kts</span>
<span class="col-4 border"><span id="mph">速度迈</span> mph</span>
<span class="col-4 border"><span id="kmh">速度千米时</span> km/h</span>
</div>
</div>
<textarea id="answer2" autocomplete="off"></textarea>
<textarea id="answer" autocomplete="off"></textarea>
</div>
<div id="config_page" class="page" style="display: none;">
<div class="row">
<div class="col-4">
<input id="accurate_chk" type="checkbox" name="accurate" value="high"
autocomplete="off" onclick="accurate_clicked(event);">
<label for="accurate" onclick="accurate_clicked(event);">非精确</label>
</div>
<div class="col-4">
<input id="line_lim_chk" type="checkbox" name="line_lim"
autocomplete="off" onclick="line_lim_clicked(event);">
<label for="line_lim" onclick="line_lim_clicked(event);">
不限制文本为10行
</label>
</div>
<div class="col-4">
<input id="local_save_chk" type="checkbox" name="local_save"
autocomplete="off" onclick="local_save_clicked();">
<label for="local_save" onclick="local_save_clicked();">
禁用本地储存
</label>
</div>
</div>
<div class="row">
<button class="col-4" onclick="show_storage();">显示本地储存</button>
<button class="col-2" onclick="show_storage_range();">分段显示本地储存</button>
<button class="col-2" onclick="clear_storage();">清除本地储存</button>
<button class="col-4" onclick="clear_storage_text();">清除文本</button>
</div>
<textarea id="answer3" autocomplete="off"></textarea>
</div>
<div id="lower_btns" class="row"></div>
<script type="text/javascript">
// Initialize
lower_btns.innerHTML = upper_btns.innerHTML;
// Definitions
const LABELS = ['la', 'lo', 'al', 'ac', 'aa', 'he', 'sp'];
const EXTRACT_DATA = (x) => [
x.latitude, x.longitude, x.altitude, x.accuracy, x.altitudeAccuracy,
x.heading, x.speed];
const DISPLAY_ACCURACY = [0, 0, 1000, 1000, 1000, 1000, 10];
// Helper functions
function gebi(id) {
return document.getElementById(id)
}
function unit_convert(value, multiplier) {
// Convert value (e.g. speed in m/s) to a different unit (e.g. mph, kts)
if (value === null) {
return "null";
} else {
return Math.round(value * multiplier * 10) / 10;
}
}
function format_degree(raw_data) {
// 123.101 -> "123°06'03''.599" (similar to "%02d°%02d'%02d''%.3f")
let sgn = raw_data < 0 ? '-' : '';
let rem = raw_data < 0 ? -raw_data : raw_data;
let deg = Math.trunc(rem);
rem = (rem - deg) * 60.0;
let min = Math.trunc(rem);
rem = (rem - min) * 60.0;
let sec = Math.trunc(rem);
rem = (rem - sec) * 1000.0;
let point = Math.trunc(rem);
return (sgn + deg + '°' + min.toString().padStart(2, '0')
+ "'" + sec.toString().padStart(2, '0')
+ "''." + point.toString().padStart(3, '0'))
}
function data2str(time, data) {
// Convert data to string (for the user to copy)
let ans = 't:' + time;
for (let i = 0; i < LABELS.length; i += 1) {
let str = 'null';
let display_str = 'null';
const label = LABELS[i];
const value = data[i];
if (!isNaN(value) && value != null) {
str = value.toString();
}
ans += ',' + label + ':' + str;
}
ans += '\n';
return ans;
}
function display_data(data) {
// Display a data
for (let i = 0; i < LABELS.length; i += 1) {
let display_str = 'null';
const label = LABELS[i];
const value = data[i];
const round = DISPLAY_ACCURACY[i];
if (!isNaN(value) && value != null) {
if (round != 0) {
display_str = Math.round(value * round) / round;
} else {
display_str = value.toString();
}
}
gebi(label).innerText = display_str;
}
gebi('la').innerText = format_degree(data[0]);
gebi('lo').innerText = format_degree(data[1]);
gebi('ft').innerText = unit_convert(data[2], 3.2808399);
gebi('kts').innerText = unit_convert(data[6], 1.94384449);
gebi('mph').innerText = unit_convert(data[6], 2.23693629);
gebi('kmh').innerText = unit_convert(data[6], 3.6);
}
function data2bytes(data) {
// Convert data (a list of doubles) to bytes
const f64arr = new Float64Array(data);
const i16arr = new Int16Array(f64arr.buffer);
let isnan = 0;
for (let i = 0; i < data.length; i++) {
isnan <<= 1;
if (isNaN(data[i]) || data[i] == null) {
isnan |= 1;
}
}
bytes = String.fromCharCode(isnan);
for (let i = 0; i < i16arr.length; i++) {
bytes += String.fromCharCode(i16arr[i]);
}
return bytes;
}
function bytes2data(bytes) {
// Convert bytes to data (a list of doubles)
let isnan = bytes.charCodeAt(0);
let i16 = [];
for (let i = 1; i < bytes.length; i++) {
i16[i16.length] = bytes.charCodeAt(i);
}
const i16arr = new Int16Array(i16);
const f64arr = new Float64Array(i16arr.buffer);
let data = Array.from(f64arr);
for (let i = data.length - 1; i >= 0; i--) {
if (isnan & 1) {
data[i] = NaN;
}
isnan >>= 1;
}
return data;
}
// Handle UI change functions
let accurate = true;
function accurate_clicked(event) {
gebi("accurate_chk").checked = accurate;
accurate = !accurate;
}
let line_lim = true;
function line_lim_clicked() {
gebi("line_lim_chk").checked = line_lim;
line_lim = !line_lim;
}
let local_save = true;
function local_save_clicked() {
gebi("local_save_chk").checked = local_save;
local_save = !local_save;
}
function toggleConfig() {
const config = gebi('config_page').style['display'] == 'none';
const cv = config ? '' : 'none';
const mv = config ? 'none' : '';
gebi('config_page').style.setProperty('display', cv);
gebi('main_page').style.setProperty('display', mv);
}
function answer_scroll() {
if (answer2.scrollTop != 0) {
answer2.scrollTop = 0;
} else {
answer2.scrollTop = answer2.scrollHeight;
}
if (answer3.scrollTop != 0) {
answer3.scrollTop = 0;
} else {
answer3.scrollTop = answer3.scrollHeight;
}
}
function swap_info() {
answer2.value += answer.value;
answer.value = '';
}
function clear_storage_text() {
answer3.value = '';
}
// Handle local storage
const LOCAL_KEY_PREFIX = 'LocationTracker:';
const LOCAL_KEY_LEN = LOCAL_KEY_PREFIX.length;
function store_local_data(t, s, i) {
if (!local_save)
return;
try {
window.localStorage.setItem(LOCAL_KEY_PREFIX + t + '|' + i, s);
} catch (error) {
answer3.value += 'Error storing to local storage at ' + t + '\n';
}
}
function collect_storage() {
// Collect relevant data in local storage
let values = [];
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (key.startsWith(LOCAL_KEY_PREFIX)) {
const [time, index] = key.substr(LOCAL_KEY_LEN).split('|');
const data = bytes2data(window.localStorage.getItem(key));
values[values.length] = [
parseInt(time), parseInt(index), data2str(time, data)];
}
}
function cmp(first, second) {
if (first[0] < second[0]) return -1;
else if (first[0] > second[0]) return 1;
else if (first[1] < second[1]) return -1;
else if (first[1] > second[1]) return 1;
else return 0;
}
values.sort(cmp);
return values;
}
function show_storage() {
const values = collect_storage();
answer3.value += (
'# Begin of local storage (' + values.length + ' lines)\n');
for (let i = 0; i < values.length; i++) {
answer3.value += values[i][2];
}
answer3.value += (
'# End of local storage (' + values.length + ' lines)\n');
}
function show_storage_range() {
const command = answer3.value.trim();
matched = command.match(/^(\d+)-(\d+)$/)
if (matched != null) {
const begin = parseInt(matched[1]);
const end = parseInt(matched[2]);
const values = collect_storage();
answer3.value = '# #lines before slice: ' + values.length + '\n';
answer3.value += (
'# Begin of local storage (start at line ' + begin + ')\n');
for (let i = 0; i < values.length; i++) {
if (begin <= i && i < end) {
answer3.value += values[i][2];
}
}
answer3.value += (
'# End of local storage (end at line ' + end + ')\n');
} else if (command == 'len') {
answer3.value = 'Length: ' + collect_storage().length + '\n';
} else {
answer3.value = 'Enter command in this input box:\n';
answer3.value += ' len:\tshow total number of lines (current '
answer3.value += 'implementation is inefficient)\n';
answer3.value += ' 0-100:\tshow first 100 lines\n';
answer3.value += ' 100-200:\tshow second 100 lines\n';
answer3.value += ' ...\n';
}
}
function clear_storage() {
if (confirm('Are you sure to clear local storage?')) {
// window.localStorage.clear();
let keys = [];
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (key.startsWith('LocationTracker:')) {
keys[keys.length] = key;
}
}
for (let i = 0; i < keys.length; i++) {
window.localStorage.removeItem(keys[i]);
}
answer3.value += '# Done (removed ' + keys.length + ' lines)\n';
}
}
// Fetch and display location data
let watchLocation_id = undefined;
let record_index = 0;
function showPosition(position) {
const coords = position.coords;
const time = Date.now().toString();
const data = EXTRACT_DATA(coords);
const str = data2str(time, data);
display_data(data);
answer.value += str;
// Save data to localStorage
store_local_data(time, data2bytes(data), record_index);
record_index++;
// Limit text length
if (line_lim) {
const arr = answer.value.split("\n")
answer.value = arr.slice(Math.max(arr.length - 10, 0)).join("\n");
}
}
function exit_refrain() {
window.onbeforeunload = function () {
return "Are you sure to close the window?";
};
}
function getPositionOptions() {
// https://developer.mozilla.org/zh-CN/docs/Web/API/PositionOptions
return { enableHighAccuracy: accurate };
}
function watchLocation() {
exit_refrain();
if (watchLocation_id !== undefined) {
alert('即将取消上一次追踪,并重新请求追踪');
navigator.geolocation.clearWatch(watchLocation_id);
watchLocation_id = undefined;
}
if (navigator.geolocation) {
watchLocation_id = (
navigator.geolocation.watchPosition(showPosition, showError,
getPositionOptions()));
}
}
function unwatchLocation() {
if (watchLocation_id !== undefined) {
navigator.geolocation.clearWatch(watchLocation_id);
watchLocation_id = undefined;
alert('已取消上一次追踪');
}
}
function showError(error) {
var error_type = 'UNDEFINED';
switch(error.code) {
case error.PERMISSION_DENIED:
error_type = 'PERMISSION_DENIED';
case error.POSITION_UNAVAILABLE:
error_type = 'POSITION_UNAVAILABLE';
case error.TIMEOUT:
error_type = 'TIMEOUT';
case error.UNKNOWN_ERROR:
error_type = 'UNKNOWN_ERROR';
}
answer.value += '## watchPosition(): ' + error_type + '\n';
}
</script>
</body>