-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.html
188 lines (167 loc) · 6.57 KB
/
ingress.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
<html>
<head>
<title>Distance/Time To Action</title>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<script type="text/javascript" src="/lib/foundation/js/foundation.min.js"></script>
<link type="text/css" rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<link type="text/css" rel="stylesheet" href="/lib/foundation/css/foundation.css">
<script type="text/javascript">
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
function postMessage(msg, isError) {
document.getElementById("message").innerHTML = msg;
$("#message-container").removeClass('hide');
}
function postDebugMessage(msg) {
debugMsgIdx++;
if (debugMsgIdx == 1)
document.getElementById("debug-message").innerHTML = "<hr><h4>Debugging:</h4><br>";
document.getElementById("debug-message").innerHTML += "<h4><small>" + debugMsgIdx + ": " + msg + "</small></h4>";
}
function getLocation(index) {
postDebugMessage('getlocation' + index);
if (index && navigator.geolocation) {
currentIndex = index;
navigator.geolocation.getCurrentPosition(showPosition, function (e) {
postMessage("Error: Unable to get your location. Is your GPS turned on?");
postDebugMessage('error: no gps found' + e);
});
} else {
postMessage("Error: Geolocation is not supported by your browser.");
postDebugMessage('error: no gps support');
}
}
function showPosition(position) {
postDebugMessage(position.coords.latitude + "," + position.coords.longitude);
var loc = $("#location"+currentIndex+"-coordinates span")[0];
loc.innerHTML = position.coords.latitude + ", " + position.coords.longitude;
var ts = $("#location"+currentIndex+"-timestamp span")[0];
var timestamp = moment();
ts.innerHTML = timestamp.format("DD MMMM YYYY, HH:mm:ss");
locations[currentIndex] = [position, timestamp];
}
function calculateDistance(pos1, pos2) {
var R = 6371; //km
var dLat = (pos2.coords.latitude - pos1.coords.latitude).toRad();
var dLng = (pos2.coords.longitude - pos1.coords.longitude).toRad();
var lat1 = pos1.coords.latitude.toRad();
var lat2 = pos2.coords.latitude.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLng/2) * Math.sin(dLng/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return R * c;
}
function calculate() {
var distance = calculateDistance(locations[1][0], locations[2][0]);
var readableDistance = distance.toString().split('.');
if (readableDistance.length > 1)
readableDistance[1] = readableDistance[1].substring(0, 3);
$("#time-to-action #distance span")[0].innerHTML = readableDistance.join('.') + " KM";
postDebugMessage("distance diff in km: " + distance);
var originTime = locations[1][1];
var speed = $("#speed")[0].value;
var timeNeeded = Math.ceil(distance / speed * 60 * 60 * 1000); // in miliseconds
var arrivalTime = moment(originTime); //clone moment obj
arrivalTime.add("ms", timeNeeded);
$("#time-to-action #estimated-time span")[0].innerHTML = arrivalTime.format('DD MMMM YYYY, HH:mm:ss') +
" (" + arrivalTime.from(moment()) + " OR " + arrivalTime.diff(moment(), 'minutes') + " minutes)";
postDebugMessage("time diff in ms: " + arrivalTime.diff(originTime));
postDebugMessage("origin: " + originTime.format());
postDebugMessage("arrivalTime: " + arrivalTime.format());
postDebugMessage("timeneeded: " + timeNeeded + "ms");
}
function reset() {
locations = [];
debugMsgIdx = 0;
postMessage("");
$("#debug-message")[0].innerHTML = "";
$("#message-container").addClass('hide');
$("#speed")[0].value = "60";
$('span').each(function (ix, d){d.innerHTML = ""});
}
var currentIndex = 0, debugMsgIdx = 0;
var locations = [];
</script>
</head>
<body>
<div class="row">
<div class="large-12 columns">
<nav class="top-bar" data-topbar>
<ul class="title-area">
<li class="name">
<h1><a href="#">Location/Time to Action</a></h1>
</li>
</ul>
<section class="top-bar-section">
<!-- Right Nav Section -->
<ul class="right">
</ul>
<!-- Left Nav Section -->
<ul class="left">
</ul>
</section>
</nav>
<br />
<div id="message-container" class="row hide">
<div id="message" data-alert class="alert-box alert hide">
<a href="#" class="close">×</a>
</div>
</div>
<div class="row">
<div class="small-12 large-6 columns">
<h2>Origin</h2>
<button type="button" class="button expand radius" id="btn-location1" onClick="javascript:getLocation(1)">Get Current Location</button>
<dl id="location1">
<div id="location1-coordinates" class="coordinates">Location: <span></span></div>
<div id="location1-timestamp" class="timestamp">Timestamp: <span></span></div>
</dl>
</div>
<div class="small-12 large-6 columns">
<h2>Destination</h2>
<button type="button" class="button expand radius" id="btn-location2" onClick="javascript:getLocation(2)">Get Current Location</button>
<dl id="location2">
<div id="location2-coordinates" class="coordinates">Location: <span></span></div>
<div id="location2-timestamp" class="timestamp">Timestamp: <span></span></div>
</dl>
</div>
</div>
<div class="row">
<div class="small-12 large-6 columns">
<h2>Speed (KM)</h2>
<p>Select speed: </p>
<select id="speed">
<option value="20">20KM/h</option>
<option value="40">40KM/h</option>
<option value="60" selected>60KM/h</option>
<option value="80">80KM/h</option>
<option value="100">100KM/h</option>
</select>
</div>
<div class="small-12 large-6 columns">
<h2>Time to Action</h2>
<div class="button-bar">
<ul class="button-group radius">
<li><a href="#time-to-action" class="button" onClick="javascript:calculate()">Calculate</a></li>
<li><a href="#" class="button alert" onClick="javascript:reset();">Reset</a></li>
</ul>
</div>
<div id="time-to-action">
<dl id="distance">
<dt>Distance: </dt>
<span>0KM</span>
</dl>
<dl id="estimated-time">
<dt>Estimated arrival to destination: </dt>
<span>N/A</span>
</dl>
</div>
</div>
</div>
<div class="row">
<div id="debug-message"></div>
</div>
</div>
</div>
</body>
</html>