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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="/static/pico.min.css" />
</head>
<body>
<div class="container">
<h1>Location Logger</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Latitude</td>
<td id="value-latitude"></td>
</tr>
<tr>
<td>Longitude</td>
<td id="value-longitude"></td>
</tr>
<tr>
<td>Accuracy</td>
<td id="value-accuracy"></td>
</tr>
<tr>
<td>Heading</td>
<td id="value-heading"></td>
</tr>
</tbody>
</table>
<div class="grid">
<button id="log" type="button">Log</button>
</div>
<progress id="progress" />
</div>
<script>
const logger_state = {
latitude: 0,
longitude: 0,
accuracy: 0,
timestamp: 0,
heading: 0,
setup: false,
inprogress: false,
};
function start() {
}
async function log_post() {
try {
const response = await fetch("/api/location", {
method: "POST",
body: JSON.stringify({
timestamp: logger_state.timestamp / 1000.0, // millis to seconds
latitude: logger_state.latitude,
longitude: logger_state.longitude,
accuracy: logger_state.accuracy,
heading: logger_state.heading,
}),
});
const response_body = await response.text();
if (response.status != 200) {
alert(`post failed\nstatus = ${response.status}\n${response_body}`);
}
} catch (err) {
alert(`post failed: ${err}`);
}
logger_state.inprogress = false;
update_ui();
}
function logger_setup() {
if (DeviceOrientationEvent.requestPermission) {
DeviceOrientationEvent.requestPermission().then(() => {
window.addEventListener("deviceorientation", (event) => {
logger_state.heading = event.webkitCompassHeading;
update_ui();
});
log();
}).catch((err) => {
alert(`failed to get device orientation permissions: ${err}`)
})
}
navigator.geolocation.watchPosition((position) => {
console.log(position);
logger_state.latitude = position.coords.latitude;
logger_state.longitude = position.coords.longitude;
logger_state.accuracy = position.coords.accuracy;
logger_state.timestamp = position.timestamp;
update_ui();
}, (err) => {
alert(`failed to get position: ${err}`);
}, {enableHighAccuracy: true});
logger_state.setup = true;
}
function log() {
if (logger_state.inprogress)
return;
if (!logger_state.setup) {
logger_setup();
return;
}
logger_state.inprogress = true;
log_post();
update_ui();
}
function update_ui() {
set_table_value("value-latitude", logger_state.latitude);
set_table_value("value-longitude", logger_state.longitude);
set_table_value("value-accuracy", logger_state.accuracy);
set_table_value("value-heading", logger_state.heading);
const progress_display = logger_state.inprogress ? "block" : "none";
document.getElementById("progress").style.display = progress_display;
}
function set_table_value(id, value) {
document.getElementById(id).innerHTML = value;
}
function main() {
document.getElementById("log").onclick = log;
update_ui();
}
window.addEventListener("load", main);
</script>
</body>
</html>
|