-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathview.html.template
256 lines (238 loc) · 9.8 KB
/
view.html.template
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
<!DOCTYPE html>
<html>
<head>
<title>${tracker_name} location</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="map"></div>
<script>
var map;
var INITIAL_API = '${location_history_url}';
var POLL_API = '${location_updates_url}';
var POLL_INTERVAL_MS = 10000;
// Timestamp of last received entry, used to poll for updates
var last_published_at;
// Current location, used to draw a continued path after receiving an update
var cur_loc;
var cur_t;
// Marker objects indicating current location, saved for deletion when
// the current location changes
var cur_markers = [];
console.log("Initial fetch");
$.ajax(INITIAL_API, {
method: 'GET',
data: {},
headers:{},
success: drawPaths
});
function drawPaths(data, status, jqXHR) {
// timestamp -> {'point': {'lat': x, 'lng': y}, 'speed': v, 'accuracy': a, 'status': s}
var items = {};
function formatTimestamp(t) {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
var d = new Date(t);
return d.getFullYear() +
'-' + pad(d.getMonth() + 1) +
'-' + pad(d.getDate()) +
' ' + pad(d.getHours()) +
':' + pad(d.getMinutes()) +
':' + pad(d.getSeconds());
}
data["Items"].forEach(function(item) {
if ("event" in item && (item["event"]["S"] == "g" || item["event"]["S"] == "cl")) {
var elems = item["data"]["S"].split(",");
if (elems.length >= 3) {
var time = formatTimestamp(item["published_at"]["S"]);
var lat = parseFloat(elems[0]);
var lng = parseFloat(elems[1]);
var acc = parseFloat(elems[2]);
var speed = -1.0;
var voltage = 0.0;
if (item["event"]["S"] == "g") {
speed = parseFloat(elems[3]);
voltage = parseFloat(elems[4]);
} else {
voltage = parseFloat(elems[3]);
}
if (!(time in items)) {
items[time] = {};
}
$.extend(items[time], {'point': {'lat': lat, 'lng': lng}, 'speed': speed, 'accuracy': acc, 'voltage': voltage});
}
} else if ("event" in item && (item["event"]["S"] == "s")) {
var time = formatTimestamp(item["published_at"]["S"]);
var s = item["data"]["S"];
if (!(time in items)) {
items[time] = {};
}
$.extend(items[time], {'status': s});
}
});
var sortedKeys = Object.keys(items).sort().filter(function(t, i) {
return (new Date().getTime() - (24 * 60 * 60 * 1000)) < new Date(t).getTime();
});
function formatItem(t, isHTML) {
var nl = isHTML ? '<br>' : '\n';
var result = t;
if ('speed' in items[t]) {
result += nl + 'speed: ' + items[t]['speed'] + ' mph';
}
if ('status' in items[t]) {
result += nl + 'status: ' + items[t]['status'];
}
if ('voltage' in items[t]) {
result += nl + 'charge: ' + items[t]['voltage'] + ' volts';
}
return result;
}
// Draw points
console.log("Drawing " + sortedKeys.length + " items");
function accuracyToCircleOpacity(acc) {
return Math.min(Math.max(10 / acc, 0.1), 0.8);
}
sortedKeys.forEach(function(t, i) {
new google.maps.Marker({
position: items[t]['point'],
title: formatItem(t, false),
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 2,
strokeWeight: 1,
strokeColor: 'white',
strokeOpacity: 1.0,
fillColor: 'black',
fillOpacity: 1.0,
}
});
new google.maps.Circle({
center: items[t]['point'],
radius: items[t]['accuracy'],
map: map,
fillColor: 'gray',
fillOpacity: accuracyToCircleOpacity(items[t]['accuracy']),
strokeColor: 'gray',
strokeOpacity: 0,
});
// Highlight points with status reports
if ('status' in items[t]) {
new google.maps.Marker({
position: items[t]['point'],
title: formatItem(t, false),
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 5,
strokeColor: 'green',
}
});
}
});
// Draw solid lines between continuously-reported locations and dashed lines between
// locations separated by a gap in time
var prev_t = cur_t;
var prev_loc = cur_loc;
sortedKeys.forEach(function(t, i) {
if (prev_t && prev_loc) {
if (new Date(t).getTime() > new Date(prev_t).getTime() + (20 * 1000)) {
new google.maps.Polyline({
path: [prev_loc, items[t]['point']],
map: map,
strokeColor: '#F00'
})
} else {
new google.maps.Polyline({
path: [prev_loc, items[t]['point']],
map: map,
})
}
}
prev_t = t;
prev_loc = items[t]['point'];
});
// Highlight last location
if (sortedKeys.length > 0) {
// Delete markers highlighting previous last location
cur_markers.forEach(function(m) { m.setMap(null); });
cur_markers = [];
var lastT = sortedKeys[sortedKeys.length - 1];
cur_markers.push(new google.maps.Marker({
position: items[lastT]['point'],
title: formatItem(lastT, false),
map: map,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 5,
strokeColor: 'red',
}
}));
cur_markers.push(new google.maps.Circle({
center: items[lastT]['point'],
radius: items[lastT]['accuracy'],
map: map,
fillColor: 'cyan',
fillOpacity: 0.1,
strokeColor: 'cyan',
strokeOpacity: 0.4,
}));
cur_markers.push(new google.maps.InfoWindow({
position: items[lastT]['point'],
content: formatItem(lastT, true),
map: map,
}));
map.panTo(items[lastT]['point']);
cur_t = lastT;
cur_loc = items[lastT]['point'];
}
// Poll for updates
if (data["Items"].length > 0) {
last_published_at = data["Items"][0]["published_at"]["S"];
}
window.setTimeout(poll, POLL_INTERVAL_MS);
}
function poll() {
if (last_published_at) {
console.log("Polling for items after " + last_published_at);
$.ajax(POLL_API, {
method: 'GET',
data: {'last_published_at': last_published_at},
headers: {},
success: drawPaths
});
} else {
console.log("Retrying initial fetch");
$.ajax(INITIAL_API, {
method: 'GET',
data: {},
headers: {},
success: drawPaths
});
}
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'),
{center: {lat: 37.876683, lng: -122.256606}, zoom: 13});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=${google_api_key}&callback=initMap"
async defer></script>
</body>
</html>