-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
423 lines (351 loc) · 15.6 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Road Condition Service</title>
<script src="http://maps.google.com/maps/api/js?sensor=false&v=3.6"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://openlayers.org/dev/OpenLayers.js" type="text/javascript"></script>
<script src="js/highcharts.js"></script>
<script src="js/modules/exporting.js"></script>
<style type="text/css">
.smallmap {
margin: 0;
width: 50%;
height: 400px;
z-index: 1;
}
.olControlAttribution {
bottom: 5px;
}
#tools {
position: absolute;
bottom: 1em;
left: 1em;
width: 10%;
z-index: 20000;
background-color: white;
opacity: 0.8;
padding: 0 0.5em 0.5em 0.5em;
}
.olImageLoadError {
display: none;
}
</style>
<script type="text/javascript">
var map;
var vector, vector2;
var geolocate;
var firstGeolocation;
var previousPoint;
var tremorArray = new Array();
var baseline = -10;
var projection = new OpenLayers.Projection("EPSG:900913");
var displayProjection = new OpenLayers.Projection("EPSG:4326");
var coords = { point: { x: -1, y: 50 }, position: { coords: { accuracy: 10 } } };
var baselineTremor = 0;
var maxTremor = 0;
var minTremor = 0;
if (window.DeviceMotionEvent) {
var x;
var y;
var z;
window.addEventListener('devicemotion', function (event) {
document.getElementById('resultDeviceMotion').innerHTML = '';
if (typeof (event.accelerationIncludingGravity) != 'undefined') {
x = event.accelerationIncludingGravity.x;
y = event.accelerationIncludingGravity.y;
z = event.accelerationIncludingGravity.z;
} else if (typeof (event.acceleration) != 'undefined') {
x = event.acceleration.x;
y = event.acceleration.y;
z = event.acceleration.z;
} else if (typeof (event.rotationRate) != 'undefined') {
x = event.rotationRate.alpha;
y = event.rotationRate.beta;
z = event.rotationRate.gamma;
}
document.getElementById('resultDeviceMotion').innerHTML = document.getElementById('resultDeviceMotion').innerHTML + "x: " + x + "<br>";
document.getElementById('resultDeviceMotion').innerHTML = document.getElementById('resultDeviceMotion').innerHTML + "y: " + y + "<br>";
document.getElementById('resultDeviceMotion').innerHTML = document.getElementById('resultDeviceMotion').innerHTML + "z: " + z + "<br>";
var value = (x + y + z) / 3;
tremorArray.push(value);
document.getElementById('resultDeviceMotion').innerHTML = document.getElementById('resultDeviceMotion').innerHTML + "Average: " + value + "<br>";
if (value < minTremor) {
minTremor = value;
document.getElementById('output').innerHTML = "New Min";
}
if (value > maxTremor) {
maxTremor = value;
document.getElementById('output').innerHTML = "New Max";
}
}, true);
}
function average(array) {
var av = 0;
var cnt = 0;
var len = array.length;
for (var i = 0; i < len; i++) {
var e = +array[i];
if (!e && array[i] !== 0 && array[i] !== '0') e--;
if (array[i] == e) { av += e; cnt++; }
}
return av / cnt;
}
var style = {
fillColor: '#000',
fillOpacity: 0.1,
strokeWidth: 0
};
function init() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
var series;
$('#container').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function () {
// set up the updating of the chart each second
series = this.series[0];
//setInterval(function() {
// var x = (new Date()).getTime(), // current time
// y = Math.random();
// series.addPoint([x, y], true, true);
//}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function () {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
baselineTremor = 0;
maxTremor = 0;
minTremor = 0;
var mercator = new OpenLayers.Projection("EPSG:900913");
var geographic = new OpenLayers.Projection("EPSG:4326");
map = new OpenLayers.Map({
div: "map",
projection: "EPSG:900913",
displayProjection: "EPSG:4326",
units: "m",
numZoomLevels: 25,
maxResolution: 156543.0339,
maxExtent: [-20037508, -20037508, 20037508, 20037508]
});
var googleSatellite = new OpenLayers.Layer.Google(
"Google Streets",
{
type: google.maps.MapTypeId.STREET,
sphericalMercator: true,
projection: "EPSG:900913",
units: 'm'
}
);
vector = new OpenLayers.Layer.Vector('marker'
//, {
// styleMap: new OpenLayers.StyleMap({
// fillColor: '$[attlineColor]',
// fillOpacity: 0.8,
// strokeColor: '$[attlineColor]',
// strokeWidth: 4
//})
//}
);
vector2 = new OpenLayers.Layer.Vector('path');
geolocate = new OpenLayers.Control.Geolocate({
bind: false,
geolocationOptions: {
enableHighAccuracy: false,
maximumAge: 0,
timeout: 7000
}
});
map.addControl(geolocate);
firstGeolocation = true;
geolocate.events.register("locationupdated", geolocate, function (e) {
document.getElementById('output').innerHTML = "";
var diffMax = Math.abs(baselineTremor) - Math.abs(maxTremor);
var diffMin = Math.abs(baselineTremor) - Math.abs(minTremor);
var result = 1;
if (Math.abs(diffMax) <= Math.abs(diffMin)) {
result = Math.abs(diffMin);
} else { result = Math.abs(diffMax) }
maxTremor = baselineTremor;
minTremor = baselineTremor;
var x = (new Date()).getTime(), // current time
// y = Math.random();
y = result;
series.addPoint([x, y], true, true);
//document.getElementById('info').innerHTML = "Result = " + result +"</br>";
vector.removeAllFeatures();
var lineFillColour = "#000000";
document.getElementById('noofmotions').innerHTML = "Result: " + result + "</br>";
if (result <= 2 && result >= 1) { //high bump - red
lineFillColour = "#C6140C";
}
if (result <= 3 && result >= 2) {
lineFillColour = "#993129";
}
if (result <= 4 && result >= 3) {
lineFillColour = "#964A29";
}
if (result <= 5 && result >= 4) {
lineFillColour = "#967F29";
}
if (6 >= result && result >= 5) {
lineFillColour = "#967F29";
}
if (7 >= result && result >= 6) {
lineFillColour = "#8BBD1E";
}
if (8 >= result && result >= 7) {
lineFillColour = "#6AD97B";
}
if (9 >= result && result >= 8) {
lineFillColour = "#53D0ED";
}
if (10 >= result && result >= 9) {
lineFillColour = "#53F1ED"; // low bump - blue
}
if (11 >= result && result >= 10) {
lineFillColour = "#53F1ED"; // low bump - blue
}
var lineStyle = {
strokeColor: lineFillColour,
fillOpacity: 0.8,
strokeWidth: 4
};
if (previousPoint != undefined) {
var pointArray = [new OpenLayers.Geometry.Point(previousPoint.x, previousPoint.y), new OpenLayers.Geometry.Point(e.point.x, e.point.y)];
var linestring = new OpenLayers.Geometry.LineString(pointArray);
var line = new OpenLayers.Feature.Vector(linestring, { attlineColor: lineFillColour }, lineStyle);
vector2.drawFeature(line);
vector2.addFeatures([line]);
}
var circle = new OpenLayers.Feature.Vector(
OpenLayers.Geometry.Polygon.createRegularPolygon(
new OpenLayers.Geometry.Point(e.point.x, e.point.y),
e.position.coords.accuracy / 2,
40,
0
),
{},
style
);
var tempPoint = new OpenLayers.Geometry.Point(e.point.x, e.point.y);
vector.addFeatures([
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(e.point.x, e.point.y),
{},
{
graphicName: 'cross',
strokeColor: '#f00',
strokeWidth: 2,
fillOpacity: 0,
pointRadius: 10
}
), circle
]);
// if (firstGeolocation == true) {
// map.zoomToExtent(vector.getDataExtent());
// firstGeolocation = false;
// this.bind = true;
// previousPoint = e.point;
// }
//map.zoomToExtent(vector.getDataExtent());
map.panTo(new OpenLayers.LonLat(e.point.x, e.point.y));
previousPoint = e.point;
}
);
geolocate.events.register("locationfailed", this, function () {
OpenLayers.Console.log('Location detection failed');
});
map.addLayers([googleSatellite, vector, vector2]);
//map.zoomToScale(5000.0);
map.setCenter(new OpenLayers.LonLat(-0.33, 51.857593).transform(geographic, mercator), 8);
map.addControl(new OpenLayers.Control.LayerSwitcher());
}
function locateme() {
vector.removeAllFeatures();
// window.setInterval(doStuff, 3000);
geolocate.deactivate();
geolocate.watch = true;
firstGeolocation = true;
geolocate.activate();
};
function calibrate() {
if (tremorArray.length > 0) {
baselineTremor = average(tremorArray);
}
document.getElementById('calibMessage').style.visibility = "visible";
}
</script>
</head>
<body onload="init()">
<div id="map" class="smallmap">
</div>
<div id='tools'>
<button id="calibrate" onclick="calibrate()">Calibrate</button>
<span id="calibMessage" style="visibility: hidden;">Calibration successful. You're ready to go</span>
<button id="locate" onclick="locateme()">Start</button>
</div>
<div id="info"></div>
<div id="resultDeviceMotion" style="float: right">Info in here</div>
<div id="noofmotions">no</div>
<div id="output"></div>
<div id="container" style="width: 50%; height: 400px; position: relative; margin: 0 auto"></div>
</body>
</html>