-
Notifications
You must be signed in to change notification settings - Fork 1
/
CLLocationManager_sim.m
466 lines (391 loc) · 16 KB
/
CLLocationManager_sim.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//-----------------------------------------------------------------------------
// CLLocationManager_sim.h
//
// Created by Gary Morris on 6/26/10.
//
// Added spead and heading on 3/7/11 to simulate motion.
//-----------------------------------------------------------------------------
//
// Copyright 2011 Gary A. Morris. http://mggm.net
//
// This file is part of CLLocationManager_sim.
//
// CLLocationManager_sim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CLLocationManager_sim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
#import "CLLocationManager_sim.h"
#import "UtilitiesGeo.h"
@implementation CLLocationManager_sim
#if TARGET_IPHONE_SIMULATOR
@dynamic delegate, location;
typedef struct {
SimStateType state; // which state data
double data; // state data value
CFTimeInterval time; // time, relative to start of sim
} SimStateUpdateType;
enum { ACCURACY_INVALID = -1 }; // indicates invalid data
#define SIM_UPDATE_PERIOD (1.0)
#define SECS_PER_HR (3600)
// SIM_SCENARIO selects a set of simulator command to control how is behaves
// This is a compile time flag. It would be nice to read commands from a file
// but that's not implemented yet.
#define SIM_SCENARIO 4
static const SimStateUpdateType simStateUpdates[] = {
#if SIM_SCENARIO==0
// Scenario: original iPhone simulator just returns lat/lon of Apple HQ in Cupertino
// Take from: http://developer.apple.com/iphone/library/documentation/Xcode/Conceptual/iphone_development/125-Using_iPhone_Simulator/iphone_simulator_application.html#//apple_ref/doc/uid/TP40007959-CH9-SW10
{ simHorizontalAccuracy, 100, 0.0 },
{ simLatitude, 37.3317, 0.0 },
{ simLongitude, -122.0307, 0.0 },
#elif SIM_SCENARIO==1
// Scenario: initial state will indicate no valid data and
// valid data will appear a few seconds later and
// accuracy will improve over the next minute
// provide a location but with low accuracy
{ simHorizontalAccuracy, 1200, 3.0 }, // Got a location via cell triangulation
{ simLatitude, 33.1, 3.0 }, // latitude of Escondido
{ simLongitude, -117.1, 3.0 }, // longitude of Escondido
// improve accuracy as we acquire GPS satellites
{ simHorizontalAccuracy, 300, 30.0 }, // Got a poor location via GPS
// add vertical position as we acquired 3D GPS position
{ simHorizontalAccuracy, 30, 40.0 }, // Better
{ simVerticalAccuracy, 30, 40.0 }, // Have altitude now
{ simAltitude, 250, 40.0 }, // Escondido altitude
// finally, best accuracy
{ simHorizontalAccuracy, 10, 75.0 },
#elif SIM_SCENARIO==2
// Scenario: initial state will provide old cached data and
// valid data will appear a few seconds later and
// accuracy will improve over the next minute
{ simLatitude, 33.5, 0.0 },
{ simLongitude, -117.5, 0.0 },
{ simHorizontalAccuracy, 10, 0.0 }, // best GPS accuracy
{ simVerticalAccuracy, 30, 0.0 }, // best GPS accuracy
{ simTimestamp, -3600, 0.0 }, // simulate cached data, 3600 seconds old
// now provide a location but with low accuracy
{ simHorizontalAccuracy, 1000, 3.0 }, // cell triangulation accuracy
{ simVerticalAccuracy, -1, 3.0 }, // no altitude data now
{ simAltitude, -1, 3.0 }, // no altitude data now
{ simLatitude, 33.15, 3.0 }, // latitude of Escondido
{ simLongitude, -117.15, 3.0 }, // longitude of Escondido
{ simTimestamp, -0.1, 3.0 }, // recent timestamp, 100ms ago
// improve accuracy as we acquire GPS satellites
{ simHorizontalAccuracy, 300, 10.0 }, // Got a poor location via GPS
{ simLatitude, 33.1, 10.0 }, // latitude of Escondido
{ simLongitude, -117.1, 10.0 }, // longitude of Escondido
// add vertical position as we acquired 3D GPS position
{ simLatitude, 33.11, 20.0 }, // latitude of Escondido
{ simLongitude, -117.11, 20.0 }, // longitude of Escondido
{ simHorizontalAccuracy, 30, 20.0 }, // Better
{ simVerticalAccuracy, 30, 20.0 }, // Have altitude now
{ simAltitude, 250, 20.0 }, // Escondido altitude
// finally, best accuracy
{ simLatitude, 33.111, 30.0 }, // latitude of Escondido
{ simLongitude, -117.111, 30.0 }, // longitude of Escondido
{ simHorizontalAccuracy, 10, 30.0 },
{ simLatitude, 33.112, 40.0 }, // latitude of Escondido
{ simLongitude, -117.112, 40.0 }, // longitude of Escondido
{ simHorizontalAccuracy, 7, 40.0 },
#elif SIM_SCENARIO==3
// top left quadrant from Nordahl Station.
{ simLatitude, 33.142582, 0.0 }, // TOP LEFT QUAD
{ simLongitude, -117.126274, 0.0 },
{ simHorizontalAccuracy, 10, 0.0 }, // best GPS accuracy
{ simTimestamp, -0.1, 0.0 }, // recent timestamp, 100ms ago
{ simLatitude, 33.133167, 5.0 },
{ simLongitude, -117.109022, 5.0 }, // TOP RIGHT QUAD
{ simLatitude, 33.116634, 10.0 },
{ simLongitude, -117.119236, 10.0 }, // BOTTOM RIGHT QUAD
{ simLatitude, 33.125045, 15.0 },
{ simLongitude, -117.137346, 15.0 }, // BOTTOM LEFT QUAD
{ simLatitude, 33.121379, 20.0 }, // TOP RIGHT QUAD from Escondido Stn
{ simLongitude, -117.075634, 20.0 },
{ simLatitude, 33.104844, 25.0 }, // BOTTOM RIGHT QUAD from Escondido Stn
{ simLongitude, -117.085333, 25.0 },
#elif SIM_SCENARIO==4
// Starting at Escondido Station fly NW directly to Oceanside station
{ simHorizontalAccuracy, 5, 0.0 }, // best GPS accuracy
{ simTimestamp, -0.1, 0.0 }, // recent timestamp, 100ms ago
{ simLatitude, 33.118600, 0.0 }, // Escondido Sprinter Station
{ simLongitude, -117.092100, 0.0 },
{ simSpeed, 200, 0.0 }, // speed in knots
{ simHeading, 286.92, 0.0 }, // heading
#elif SIM_SCENARIO==5
// Starting at Oceanside Station fly SE directly to Escondido station
{ simHorizontalAccuracy, 5, 0.0 }, // best GPS accuracy
{ simTimestamp, -0.1, 0.0 }, // recent timestamp, 100ms ago
{ simLatitude, 33.191363992, 0.0 }, // Oceanside Sprinter Station
{ simLongitude, -117.378629744, 0.0 },
{ simSpeed, 500, 10.0 }, // speed in knots
{ simHeading, 106.7, 10.0 }, // heading degrees
#else
,
#error Need to provide simulation data
#endif
};
//---------------------------------------------------------------------
// dealloc
//---------------------------------------------------------------------
-(void)dealloc
{
[simTimer invalidate];
[simLocation release];
[super dealloc];
}
//---------------------------------------------------------------------
// init
//---------------------------------------------------------------------
-(id)init
{
if (self = [super init]) {
// initialize simStateData with all zeroes
for (int i=0; i < sizeof(simState)/sizeof(simState[0]); i++) {
simState[i] = 0.0;
}
simState[simHorizontalAccuracy] = ACCURACY_INVALID;
simState[simVerticalAccuracy] = ACCURACY_INVALID;
simState[simAltitude] = -1;
nextUpdate = 0;
simStartTime = -1.0; // not started yet
}
return self;
}
//---------------------------------------------------------------------
// delegate property - overrides property in CLLocationManager
//---------------------------------------------------------------------
-(void)setDelegate:(id <CLLocationManagerDelegate>)newDelegate
{
simDelegate = newDelegate;
// insert self as delegate in between
[super setDelegate: newDelegate ? self : nil];
}
-(id <CLLocationManagerDelegate>)delegate
{
return simDelegate;
}
//---------------------------------------------------------------------
// location property - overrides property in CLLocationManager
// (location property stored in simLocation ivar)
//---------------------------------------------------------------------
-(void)setLocation:(CLLocation*)newLocation
{
[newLocation retain];
[simLocation release];
simLocation = newLocation;
}
-(CLLocation*)location
{
return [[simLocation retain] autorelease];
}
//---------------------------------------------------------------------
// compute next lat/lon coordinates given speed and bearing
// from previous lat/lon location
//---------------------------------------------------------------------
-(CLLocationCoordinate2D)calcLocationFromStartLat:(double)lat1
StartLong:(double)lon1
BearingDegrees:(double)bearingDegrees
SpeedKnots:(double)knots
Seconds:(double)seconds
{
double distanceNM = knots * (seconds / SECS_PER_HR);
double distanceM = distanceNM * NM_TO_METERS_FACTOR;
CLLocationCoordinate2D result;
destCoordsInDegrees(lat1, lon1,
distanceM, bearingDegrees,
&result.latitude, &result.longitude);
return result;
}
//---------------------------------------------------------------------
// updateSimState - call periodically to update simulator state,
// processes the next state updates if it is time for them.
// returns BOOL which indicates if location changed
//---------------------------------------------------------------------
-(BOOL)updateSimState
{
const unsigned nUpdates = sizeof(simStateUpdates) / sizeof(simStateUpdates[0]);
const CFTimeInterval simTime = CFAbsoluteTimeGetCurrent() - simStartTime;
BOOL locationChanged = NO;
BOOL done = nextUpdate >= nUpdates;
if (simState[simSpeed] != 0) {
// compute new latitude and longitude
CLLocationCoordinate2D newLoc = [self calcLocationFromStartLat:simState[simLatitude]
StartLong:simState[simLongitude]
BearingDegrees:simState[simHeading]
SpeedKnots:simState[simSpeed]
Seconds:SIM_UPDATE_PERIOD];
simState[simLatitude] = newLoc.latitude;
simState[simLongitude] = newLoc.longitude;
locationChanged = YES;
}
while (!done && simStateUpdates[nextUpdate].time <= simTime) {
// perform the sim state data update, it's time has come
simState[simStateUpdates[nextUpdate].state] = simStateUpdates[nextUpdate].data;
locationChanged = YES; // an update was made
done = ++nextUpdate >= nUpdates;
}
// are we done?
if (done && simState[simSpeed]==0) {
// no more simulator commands, no more motion
[simTimer invalidate];
simTimer = nil;
}
return locationChanged;
}
//---------------------------------------------------------------------
// simulatorTask - called once per second perform next callback
//---------------------------------------------------------------------
-(void)simulatorTask
{
const BOOL locChanged = [self updateSimState];
if (!locChanged) return;
// location changed, create a new CLLocation value, call delegate
CLLocationCoordinate2D coordinates = { simState[simLatitude], simState[simLongitude] };
NSDate* timestamp = [NSDate dateWithTimeIntervalSinceNow:simState[simTimestamp]];
CLLocation* oldLoc = self.location;
CLLocation* newLoc =
[[CLLocation alloc] initWithCoordinate:coordinates
altitude:simState[simAltitude]
horizontalAccuracy:simState[simHorizontalAccuracy]
verticalAccuracy:simState[simVerticalAccuracy]
timestamp:timestamp];
// call the delegate with the new location
if (simDelegate &&
[simDelegate respondsToSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)])
{
//---------------------------
// Apply distanceFilter
//---------------------------
// (if no filter we always have moved enough)
BOOL isValid = newLoc.horizontalAccuracy >= 0;
BOOL meetsDistanceFilter = self.distanceFilter == kCLDistanceFilterNone;
if (!meetsDistanceFilter) {
if (oldLoc==nil || oldLoc.horizontalAccuracy < 0) {
// old location is not valid, filter is met if valid now
meetsDistanceFilter = isValid;
} else if (isValid) {
// compute distance from last location
CLLocationDistance distance = [newLoc distanceFromLocation:oldLoc];
meetsDistanceFilter = distance >= self.distanceFilter;
}
}
//-------------------------------------
// Deliver the location change event
//-------------------------------------
if (meetsDistanceFilter) {
[simDelegate locationManager:self didUpdateToLocation:newLoc fromLocation:oldLoc];
self.location = newLoc; // update current location
} else if (!isValid) {
// new location is invalid, send an error to the delegate
NSError* error = [NSError errorWithDomain:@"CLLocationManager"
code:kCLErrorLocationUnknown
userInfo:nil];
[simDelegate locationManager:self didFailWithError:error];
self.location = newLoc; // update current location
}
}
[newLoc release];
}
//---------------------------------------------------------------------
// start/stopUpdatingLocation
//---------------------------------------------------------------------
-(void)startUpdatingLocation
{
if (simStartTime < 0) {
simStartTime = CFAbsoluteTimeGetCurrent();
}
if (simTimer == nil) {
simTimer = [NSTimer scheduledTimerWithTimeInterval:SIM_UPDATE_PERIOD
target:self
selector:@selector(simulatorTask)
userInfo:nil
repeats:YES];
[self simulatorTask]; // initial call, subsequently called by timer
}
// start real CLLocationManager in case we are running on a
// real device we can allow it to provide region or heading
// updates which we don't simulate
[super startUpdatingLocation];
}
-(void)stopUpdatingLocation
{
[simTimer invalidate];
simTimer = nil;
[super stopUpdatingLocation];
}
//---------------------------------------------------------------------
// locationServicesEnabled
//---------------------------------------------------------------------
+(BOOL)locationServicesEnabled
{
return YES;
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
// CLLocationManagerDelegate methods
//---------------------------------------------------------------------
//---------------------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region
{
if ([simDelegate respondsToSelector:@selector(locationManager:didEnterRegion:)])
{
[simDelegate locationManager:manager didEnterRegion:region];
}
}
- (void)locationManager:(CLLocationManager *)manager
didExitRegion:(CLRegion *)region
{
if ([simDelegate respondsToSelector:@selector(locationManager:didExitRegion:)])
{
[simDelegate locationManager:manager didExitRegion:region];
}
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
// ignore errors from iPhone simulator
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
if ([simDelegate respondsToSelector:@selector(locationManager:didUpdateHeading:)])
{
[simDelegate locationManager:manager didUpdateHeading:newHeading];
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// ignore location updates from iPhone simulator
}
- (void)locationManager:(CLLocationManager *)manager
monitoringDidFailForRegion:(CLRegion *)region
withError:(NSError *)error
{
if ([simDelegate respondsToSelector:@selector(locationManager:monitoringDidFailForRegion:withError:)])
{
[simDelegate locationManager:manager monitoringDidFailForRegion:region withError:error];
}
}
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
{
if ([simDelegate respondsToSelector:@selector(locationManagerShouldDisplayHeadingCalibration:)])
{
return [simDelegate locationManagerShouldDisplayHeadingCalibration:manager];
} else {
return NO;
}
}
#endif
@end