-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocationManagerSimulator.m
182 lines (156 loc) · 5.95 KB
/
LocationManagerSimulator.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// LocationManagerSimulator.m
// BasicSatNav
//
// Created by John McKerrell on 02/12/2009.
// Copyright 2009 MKE Computing Ltd. All rights reserved.
//
#import "LocationManagerSimulator.h"
#import "CLLocation+CLExtensions.h"
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
// This basically just a random figure found here:
// http://www.developerfusion.com/article/4652/writing-your-own-gps-applications-part-2/3/
#define DOP2METERS 6.0
@implementation LocationManagerSimulator
- (id)init
{
if (self = [super init]) {
multiplier = 1.0;
filename = @"simulated-locations";
startTime = nil;
timer = nil;
oldLocation = nil;
lastDataIndex = 0;
}
return self;
}
- (id)initWithFilename:(NSString*)fn andMultiplier:(float)mult {
if( self = [self init]) {
filename = fn;
multiplier = mult;
}
return self;
}
#if TARGET_IPHONE_SIMULATOR
- (void)loadData {
if( ! filename ) {
return;
}
NSString *fullFilename = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"];
NSArray *fileData = [NSArray arrayWithContentsOfFile:fullFilename];
if( fileData ) {
NSLog( @"Loaded data from %@", fullFilename );
data = [fileData retain];
} else {
NSLog( @"Couldn't load data from %@", fullFilename );
filename = nil;
}
}
- (void)startUpdatingHeading {
}
- (void)startUpdatingLocation {
if( timer ) {
return;
}
[self loadData];
if( ! data ) {
return [super startUpdatingLocation];
}
timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(calculatePosition)
userInfo:nil
repeats:YES];
[timer retain];
startTime = [NSDate new];
}
- (void)stopUpdatingLocation {
if( ! filename ) {
return [super stopUpdatingLocation];
}
[timer invalidate];
[timer release];
timer = nil;
}
- (CLLocation*) dictToLocation:(NSDictionary*) dict {
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[dict objectForKey:@"lat"] floatValue];
coordinate.longitude = [[dict objectForKey:@"lon"] floatValue];
CLLocationDistance altitude = 0.0;
if( [dict objectForKey:@"ele"] ) {
altitude = [[dict objectForKey:@"ele"] floatValue];
}
CLLocationAccuracy hAccuracy = 50.0, vAccuracy = 50.0;
if( [dict objectForKey:@"hdop"] ) {
hAccuracy = DOP2METERS * [[dict objectForKey:@"hdop"] floatValue];
}
if( [dict objectForKey:@"vdop"] ) {
vAccuracy = DOP2METERS * [[dict objectForKey:@"vdop"] floatValue];
}
CLLocation *location = [[CLLocation alloc]
initWithCoordinate:coordinate
altitude:altitude
horizontalAccuracy:hAccuracy
verticalAccuracy:vAccuracy
timestamp:[NSDate date]];
if (oldLocation) {
CLLocationDirection course = [oldLocation bearingInRadiansTowardsLocation:location];
course = RADIANS_TO_DEGREES(course);
[location autorelease];
location = [[CLLocation alloc] initWithCoordinate:coordinate
altitude:altitude
horizontalAccuracy:hAccuracy
verticalAccuracy:vAccuracy
course:course
speed:0
timestamp:[NSDate date]];
}
return [location autorelease];
}
- (void)calculatePosition {
NSUInteger currentDataIndex;
CLLocation *location = nil;
if( ! oldLocation ) {
currentDataIndex = lastDataIndex;
location = [self dictToLocation:[data objectAtIndex:currentDataIndex]];
} else {
NSTimeInterval interval = [startTime timeIntervalSinceNow];
interval *= multiplier;
//NSLog( @"Calculate position after %f", interval );
currentDataIndex = lastDataIndex;
NSDictionary *firstGPXPoint = [data objectAtIndex:0];
NSDate *firstGPXTime = [firstGPXPoint objectForKey:@"time"];
// While we haven't reached the end of the datapoints
// if the interval between the GPX start and the GPX index position is greater than the interval between our start and now
while( currentDataIndex < [data count] ) {
NSDictionary *currentGPXPoint = [data objectAtIndex:currentDataIndex];
NSDate *currentGPXTime = [currentGPXPoint objectForKey:@"time"];
if( currentGPXTime ) {
NSTimeInterval gpxInterval = [firstGPXTime timeIntervalSinceDate:currentGPXTime];
//NSLog( @"gpxInterval=%f", gpxInterval );
if( gpxInterval < interval ) {
// We've gone past the point we wanted to be at
break;
}
}
++currentDataIndex;
}
// We'll always go past the point we want to either by reaching the end or going one past the right one
--currentDataIndex;
if( currentDataIndex != lastDataIndex ) {
location = [self dictToLocation:[data objectAtIndex:currentDataIndex]];
}
}
if( location ) {
if ([self.delegate respondsToSelector:@selector(locationManager:didUpdateLocations:)]) {
[self.delegate locationManager:self didUpdateLocations:@[location]];
}
if ([self.delegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)]){
[self.delegate locationManager:self didUpdateToLocation:location fromLocation:oldLocation];
}
lastDataIndex = currentDataIndex;
oldLocation = location;
}
}
#endif
@end