/
GPSTracePlayer.m
112 lines (85 loc) · 2.77 KB
/
GPSTracePlayer.m
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
//
// GPSTracePlayer.m
// The Race App
//
// Created by Appsterdam on 16/07/11.
// Use this code at your own risk for whatever you want.
// But if you make money out of it, please give something back to Appsterdam.
//
#import "GPSTracePlayer.h"
@interface GPSTracePlayer ()
@property(nonatomic, assign) BOOL isPlaying;
@property(nonatomic, assign) NSUInteger currentIndex;
@property(nonatomic, retain) NSDate * currentTimestamp;
@property(nonatomic, assign) NSTimeInterval logToPlayerStartDelta;
- (void)synthesizeNextEvent;
/* - */
@property(nonatomic, assign) CLLocationAccuracy desiredAccuracy;
@property(nonatomic, assign) CLLocationDistance distanceFilter;
- (void)startUpdatingLocation;
- (void)stopUpdatingLocation;
- (void)startUpdatingHeading;
- (void)stopUpdatingHeading;
@end
@implementation GPSTracePlayer
@synthesize trace;
@synthesize isPlaying;
@synthesize currentIndex;
@synthesize currentTimestamp;
@synthesize logToPlayerStartDelta;
- (CLLocationManager *)playerAsLocationManager;
{
return (CLLocationManager *)self;
}
- (void)startPlayingTrace;
{
self.isPlaying = YES;
self.currentIndex = NSNotFound;
self.currentTimestamp = nil;
self.logToPlayerStartDelta = - [self.trace.startTime timeIntervalSinceNow];
[self synthesizeNextEvent];
}
- (CLLocation *)nextEvent;
{
NSUInteger nextEventIndex = self.currentIndex + 1;
if (nextEventIndex == self.trace.count)
return nil;
return [self.trace pointAtIndex:nextEventIndex];
}
- (void)synthesizeNextEvent;
{
self.currentIndex = (self.currentIndex == NSNotFound) ? 0 : self.currentIndex + 1;
if (self.currentIndex == self.trace.count - 1)
{
self.isPlaying = NO;
return;
}
CLLocation * event = [self.trace pointAtIndex:self.currentIndex];
CLLocation * nextEvent = [self nextEvent];
CLLocation * timeRebasedEvent;;
{
timeRebasedEvent = [[CLLocation alloc] initWithCoordinate:event.coordinate
altitude:event.altitude
horizontalAccuracy:event.horizontalAccuracy
verticalAccuracy:event.verticalAccuracy
timestamp:[event.timestamp dateByAddingTimeInterval:self.logToPlayerStartDelta]];
}
[self.delegate locationManager:self.playerAsLocationManager
didUpdateToLocation:timeRebasedEvent
fromLocation:nil]; // we don't use it, so...
if (nextEvent != nil)
{
NSTimeInterval delta = [nextEvent.timestamp timeIntervalSinceDate:event.timestamp];
[self performSelector:@selector(synthesizeNextEvent)
withObject:nil
afterDelay:delta];
}
}
@synthesize desiredAccuracy;
@synthesize delegate;
@synthesize distanceFilter;
- (void)startUpdatingLocation; { }
- (void)stopUpdatingLocation; { }
- (void)startUpdatingHeading; { }
- (void)stopUpdatingHeading; { }
@end