Skip to content

Commit d4a0bca

Browse files
updated position logic
1 parent b9ec8b0 commit d4a0bca

File tree

4 files changed

+83
-76
lines changed

4 files changed

+83
-76
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h2>Chase car mode</h2>
5858
<span class="r"><input type="text" id="cc_callsign" style="width: 140px;"/></span>
5959
</div>
6060
<div class="row">
61-
<span><b>Warning: If you enable this, your location will be uploaded to habitat and be publicly visible on the map.</b></span>
61+
<span><b>Warning: If you enable this, your location will be uploaded to habitat making it publicly visible on the map.</b></span>
6262
</div>
6363
<hr>
6464
<div class="row">

js/app.js

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,82 @@ function checkSize() {
4444
window.onresize = checkSize;
4545
window.onchangeorientation = checkSize;
4646

47+
// functions
48+
49+
var positionUpdateHandle = function() {
50+
navigator.geolocation.getCurrentPosition(function(position) {
51+
var lat = position.coords.latitude;
52+
var lon = position.coords.longitude;
53+
var alt = (position.coords.altitude) ? position.coords.altitude : 0;
54+
var accuracy = (position.coords.accuracy) ? position.coords.accuracy : 0;
55+
var speed = (position.coords.speed) ? position.coords.speed : 0;
56+
57+
// constantly update 'last updated' field, and display friendly time since last update
58+
if(!GPS_ts) {
59+
GPS_ts = parseInt(position.timestamp/1000);
60+
61+
setInterval(function() {
62+
var delta_ts = parseInt(Date.now()/1000) - GPS_ts;
63+
64+
// generate friendly timestamp
65+
var hours = Math.floor(delta_ts / 3600);
66+
var minutes = Math.floor(delta_ts / 60) % 60;
67+
var ts_str = (delta_ts >= 60) ?
68+
((hours)?hours+'h ':'')
69+
+ ((minutes)?minutes+'m':'')
70+
+ ' ago'
71+
: 'just now';
72+
$('#cc_timestamp').text(ts_str);
73+
}, 30000);
74+
75+
$('#cc_timestamp').text('just now');
76+
}
77+
78+
// save position and update only if different is available
79+
if(GPS_lat != lat
80+
|| GPS_lon != lon
81+
|| GPS_alt != alt
82+
|| GPS_speed != speed)
83+
{
84+
GPS_lat = lat;
85+
GPS_lon = lon;
86+
GPS_alt = alt;
87+
GPS_speed = speed;
88+
GPS_ts = parseInt(position.timestamp/1000);
89+
$('#cc_timestamp').text('just now');
90+
91+
if(CHASE_enabled) {
92+
ChaseCar.updatePosition(callsign, position);
93+
}
94+
}
95+
else { return; }
96+
97+
// add/update marker on the map (tracker.js)
98+
updateCurrentPosition(lat, lon);
99+
100+
// round the coordinates
101+
lat = parseInt(lat * 1000000)/1000000; // 6 decimal places
102+
lon = parseInt(lon * 1000000)/1000000; // 6 decimal places
103+
speed = parseInt(speed * 10)/10; // 1 decimal place
104+
accuracy = parseInt(accuracy);
105+
alt = parseInt(alt);
106+
107+
// dispaly them in the top right corner
108+
$('#app_name b').html(lat + '<br/>' + lon);
109+
110+
// update chase car interface
111+
$('#cc_lat').text(lat);
112+
$('#cc_lon').text(lon);
113+
$('#cc_alt').text(alt + " m");
114+
$('#cc_accuracy').text(accuracy + " m");
115+
$('#cc_speed').text(speed + " m/s");
116+
},
117+
function() {
118+
// when there is no location
119+
$('#app_name b').html('mobile<br/>tracker');
120+
});
121+
}
122+
47123

48124

49125
$(window).ready(function() {
@@ -191,79 +267,10 @@ $(window).ready(function() {
191267
}
192268
});
193269

194-
setInterval(function() {
195-
navigator.geolocation.getCurrentPosition(function(position) {
196-
var lat = position.coords.latitude;
197-
var lon = position.coords.longitude;
198-
var alt = (position.coords.altitude) ? position.coords.altitude : 0;
199-
var accuracy = (position.coords.accuracy) ? position.coords.accuracy : 0;
200-
var speed = (position.coords.speed) ? position.coords.speed : 0;
201-
202-
// constantly update 'last updated' field, and display friendly time since last update
203-
if(!GPS_ts) {
204-
GPS_ts = parseInt(position.timestamp/1000);
205-
206-
setInterval(function() {
207-
var delta_ts = parseInt(Date.now()/1000) - GPS_ts;
208-
209-
// generate friendly timestamp
210-
var hours = Math.floor(delta_ts / 3600);
211-
var minutes = Math.floor(delta_ts / 60) % 60;
212-
var ts_str = (delta_ts >= 60) ?
213-
((hours)?hours+'h ':'')
214-
+ ((minutes)?minutes+'m':'')
215-
+ ' ago'
216-
: 'just now';
217-
$('#cc_timestamp').text(ts_str);
218-
}, 30000);
219-
220-
$('#cc_timestamp').text('just now');
221-
}
222-
223-
// save position and update only if different is available
224-
if(GPS_lat != lat
225-
|| GPS_lon != lon
226-
|| GPS_alt != alt
227-
|| GPS_speed != speed)
228-
{
229-
GPS_lat = lat;
230-
GPS_lon = lon;
231-
GPS_alt = alt;
232-
GPS_speed = speed;
233-
GPS_ts = parseInt(position.timestamp/1000);
234-
$('#cc_timestamp').text('just now');
235-
236-
if(CHASE_enabled) {
237-
ChaseCar.updatePosition(callsign, position);
238-
}
239-
}
240-
else { return; }
241-
242-
// add/update marker on the map (tracker.js)
243-
updateCurrentPosition(lat, lon);
244-
245-
// round the coordinates
246-
lat = parseInt(lat * 1000000)/1000000; // 6 decimal places
247-
lon = parseInt(lon * 1000000)/1000000; // 6 decimal places
248-
speed = parseInt(speed * 10)/10; // 1 decimal place
249-
accuracy = parseInt(accuracy);
250-
alt = parseInt(alt);
251-
252-
// dispaly them in the top right corner
253-
$('#app_name b').html(lat + '<br/>' + lon);
254-
255-
// update chase car interface
256-
$('#cc_lat').text(lat);
257-
$('#cc_lon').text(lon);
258-
$('#cc_alt').text(alt + " m");
259-
$('#cc_accuracy').text(accuracy + " m");
260-
$('#cc_speed').text(speed + " m/s");
261-
},
262-
function() {
263-
// when there is no location
264-
$('#app_name b').html('mobile<br/>tracker');
265-
});
266-
}, 30000);
270+
// check for location update every 30sec
271+
setInterval(positionUpdateHandle, 30000);
272+
// immediatelly check for position
273+
positionUpdateHandle();
267274
}
268275

269276
// preload images

js/mobile.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/tracker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ function refreshPredictions() {
10371037
}
10381038

10391039
var periodical, periodical_receivers, periodical_predictions;
1040-
var timer_seconds = 10;
1040+
var timer_seconds = 30;
10411041

10421042
function startAjax() {
10431043
// prevent insane clicks to start numerous requests

0 commit comments

Comments
 (0)