@@ -2585,24 +2585,29 @@ function addPosition(position) {
2585
2585
if ( vehicle . num_positions > 0 && dt > 0 ) {
2586
2586
var search_ts = new_ts - 10000
2587
2587
2588
- function searchPositions ( time ) {
2589
- return time <= search_ts
2588
+ function searchPositions ( times ) {
2589
+ for ( i = times . length ; i >= 0 ; i -- ) {
2590
+ if ( times [ i ] <= search_ts ) {
2591
+ return times [ i ]
2592
+ }
2593
+ }
2594
+ return null
2590
2595
}
2591
2596
2592
- var search_matches = vehicle . positions_ts . filter ( searchPositions )
2597
+ var search_match = searchPositions ( vehicle . positions_ts )
2598
+ var search_index = vehicle . positions_ts . indexOf ( search_match )
2593
2599
2594
- if ( search_matches . length > 0 && search_matches [ search_matches . length - 1 ] >= search_ts - 5000 ) {
2595
- var search_match = search_matches [ search_matches . length - 1 ]
2600
+ if ( search_match != null && search_match >= search_ts - 5000 ) {
2596
2601
var dtt = ( curr_ts - search_match ) / 1000 ;
2597
2602
2598
2603
// calculate vertical rate
2599
- var rate = ( position . gps_alt - vehicle . positions_alts [ search_matches . length - 1 ] ) / dtt ;
2604
+ var rate = ( position . gps_alt - vehicle . positions_alts [ search_index ] ) / dtt ;
2600
2605
if ( ! isNaN ( rate ) && isFinite ( rate ) ) {
2601
2606
vehicle . ascent_rate = 0.2 * rate + 0.8 * vehicle . ascent_rate ;
2602
2607
}
2603
2608
2604
2609
// calculate horizontal rate
2605
- horizontal_rate_temp = new_latlng . distanceTo ( vehicle . positions [ search_matches . length - 1 ] ) / dtt ;
2610
+ horizontal_rate_temp = new_latlng . distanceTo ( vehicle . positions [ search_index ] ) / dtt ;
2606
2611
if ( ! isNaN ( horizontal_rate_temp ) && isFinite ( horizontal_rate_temp ) ) {
2607
2612
vehicle . horizontal_rate = horizontal_rate_temp ;
2608
2613
}
0 commit comments