@@ -44,6 +44,82 @@ function checkSize() {
4444window . onresize = checkSize ;
4545window . 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
0 commit comments