Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LocationPredictor.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2c2e32999ef02b6b21b8fa7624c151e34526f5c4 (plain)
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
#import "LocationPredictor.h"
#import "Framework.h"

#include "../../../base/timer.hpp"

#include "../../../map/location_state.hpp"


namespace
{
  NSTimeInterval const PREDICTION_INTERVAL = 0.2; // in seconds
  int const MAX_PREDICTION_COUNT = 20;
}

@implementation LocationPredictor
{
  NSObject<LocationObserver> * m_observer;
  NSTimer * m_timer;
  
  location::GpsInfo m_lastGpsInfo;
  bool m_gpsInfoIsValid;
  
  int m_connectionSlot;
  bool m_generatePredictions;
  int m_predictionCount;
}

-(id)initWithObserver:(NSObject<LocationObserver> *)observer
{
  if ((self = [super init]))
  {
    m_observer = observer;
    m_timer = nil;
    m_gpsInfoIsValid = false;
    m_generatePredictions = false;

    m_connectionSlot = GetFramework().GetLocationState()->AddStateModeListener([self](location::State::Mode mode)
    {
      m_generatePredictions = (mode == location::State::RotateAndFollow);
      if (mode < location::State::NotFollow)
        m_gpsInfoIsValid = false;

      [self resetTimer];
    });
  }
  
  return self;
}

-(void)dealloc
{
  GetFramework().GetLocationState()->RemoveStateModeListener(m_connectionSlot);
}

-(void)reset:(location::GpsInfo const &)info
{
  if (info.HasSpeed() && info.HasBearing())
  {
    m_gpsInfoIsValid = true;
    m_lastGpsInfo = info;
    m_lastGpsInfo.m_timestamp = my::Timer::LocalTime();
    m_lastGpsInfo.m_source = location::EPredictor;
  }
  else
    m_gpsInfoIsValid = false;

  [self resetTimer];
}

-(bool)isPredict
{
  return m_gpsInfoIsValid && m_generatePredictions;
}

-(void)resetTimer
{
  m_predictionCount = 0;

  if (m_timer != nil)
  {
    [m_timer invalidate];
    m_timer = nil;
  }
  
  if ([self isPredict])
  {
    m_timer = [NSTimer scheduledTimerWithTimeInterval:PREDICTION_INTERVAL
                                               target:self
                                             selector:@selector(timerFired)
                                             userInfo:nil
                                              repeats:YES];
  }
}

-(void)timerFired
{
  if (![self isPredict])
    return;
  
  if (m_predictionCount < MAX_PREDICTION_COUNT)
  {
    ++m_predictionCount;

    location::GpsInfo info = m_lastGpsInfo;
    info.m_timestamp = my::Timer::LocalTime();
    ::Framework::PredictLocation(info.m_latitude, info.m_longitude, info.m_horizontalAccuracy, info.m_bearing,
                                 info.m_speed, info.m_timestamp - m_lastGpsInfo.m_timestamp);
    
    [m_observer onLocationUpdate:info];
  }
}

@end