-1

I am starting a timer with view did load

- (void)viewDidLoad
{
[super viewDidLoad];
locationManager.delegate = self;
[NSTimer scheduledTimerWithTimeInterval: 10.0 //interval here must be a float
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
}

then i have my timer method:

-(void)onTick:(NSTimer *)timer {
[locationManager startUpdatingLocation];

}

then the location manager is called since its a delegate:

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"made it to location manager");

NSTimeInterval timePassedSinceLastLocationUpdate = fabs([oldLocation.timestamp timeIntervalSinceNow]);

NSLog(@"update time: %f", timePassedSinceLastLocationUpdate);

NSLog(@"--------------");
//other stuff happens
[locationManager stopUpdatingLocation];
}

However, the NSLOG that is shown is this:


2012-07-31 13:02:28.092 round5[1713:f803] made it to location manager
2012-07-31 13:02:28.095 round5[1713:f803] update time: 0.000000
2012-07-31 13:02:28.095 round5[1713:f803] --------------
2012-07-31 13:02:33.298 round5[1713:f803] made it to location manager
2012-07-31 13:02:33.298 round5[1713:f803] update time: 5.222202
2012-07-31 13:02:33.300 round5[1713:f803] --------------
2012-07-31 13:02:44.086 round5[1713:f803] made it to location manager
2012-07-31 13:02:44.086 round5[1713:f803] update time: 15.986345
2012-07-31 13:02:44.087 round5[1713:f803] --------------
2012-07-31 13:02:53.297 round5[1713:f803] made it to location manager
2012-07-31 13:02:53.302 round5[1713:f803] update time: 9.217123
2012-07-31 13:02:53.303 round5[1713:f803] --------------
2012-07-31 13:03:04.096 round5[1713:f803] made it to location manager
2012-07-31 13:03:04.097 round5[1713:f803] update time: 20.007919
2012-07-31 13:03:04.098 round5[1713:f803] --------------
2012-07-31 13:03:13.297 round5[1713:f803] made it to location manager
2012-07-31 13:03:13.298 round5[1713:f803] update time: 9.202388
2012-07-31 13:03:13.300 round5[1713:f803] --------------
2012-07-31 13:03:24.111 round5[1713:f803] made it to location manager
2012-07-31 13:03:24.112 round5[1713:f803] update time: 20.012550
2012-07-31 13:03:24.113 round5[1713:f803] --------------

as you can see towards the bottom the pattern normalizes and it either reads ~10 seconds (which is right, because thats what the interval is set to and compared against the system clock)

and 20... which...i don't know where it comes from.

I guess my question is "Why isn't it reading 10 seconds all the time; why does it double the NStimer value?"

thanks a lot!

stackOverFlew
  • 1,479
  • 2
  • 31
  • 58
  • 1
    You have formatted your code using HTML markup, whereas Stack Overflow [primarily uses Markdown](http://stackoverflow.com/editing-help) for formatting. Thus, the code indentation is incorrect. This is usually easy for someone else to fix up, but you have added `
    ` at the end of every line. Could you please re-paste your code and use the preferred formatting technique? Please also [remove the tags](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles) from the title. They are redundant.
    – jscs Jul 31 '12 at 20:14

2 Answers2

5

You have two mistakes here. First, you shouldn't call startUpdatingLocation repeatedly. You should call it one time to start requesting updates as they're available (you should then call stopUpdatingLocation when you don't need updates anymore). Your second mistake is believing that calling startUpdatingLocation will always return you a new location. You're not checking the last time your NSTimer ran. You're checking the last time CLLocationManager updated the location (oldLocation.timestamp). This can be any arbitrary amount of time. It may not have been able to get an update when you asked for it last time. It may have updated at times you didn't ask for it.

Read over the Location Awareness Programming Guide. It explains how to request updates and when to request updates. Generally you turn it on and tell the system how accurate you want your updates. You then let the system call you when things change. You do not repeatedly ask for location updates.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • thx for the answer!!! The problem is, is that I have some stuff that I run in the location manager method that I cant run when the updates become available, because that is too often. So I need to receive updates at least 10 seconds apart. BUT, it would be great to know the time between each update... do you know another way I can make sure that I receive the updates not too frequently using something else besides the timer? I also need the EXACT time that passed between the location updates... is there another way to do this? thanks a lot! – stackOverFlew Aug 01 '12 at 04:20
  • btw im using the time interval to just space out the cllocation manager request at least 10 seconds apart. thats the only purpose of the nstimer... I just need to get the time passed since the last update... do you know why it's not working? because I keep stopping location manager? is there another way to do this? – stackOverFlew Aug 01 '12 at 04:26
  • 1
    Don't use a timer. In `didUpdateToLocation:`, just note the time, and if it's too soon, just wait for the next one. Your `timePassedSinceLastLocationUpdate` will tell you the time between updates. – Rob Napier Aug 01 '12 at 12:38
  • of thanks! how would I do that? store the timestamp in a global variable, then compare each time the timestamp of the newlocation the method is called? because timePassedSinceLastLocationUpdate will tell me the update that happened less than 10 seconds ago... or do I not understand something? thx a lot btw!!! – stackOverFlew Aug 01 '12 at 18:13
  • 1
    You'd store the last time you cared about in an instance variable (probably not a global variable; they're rare in ObjC). But beyond that, yes. – Rob Napier Aug 01 '12 at 19:04
  • at least i think that's why... the code and log is here: https://pastee.org/3umgh – stackOverFlew Aug 01 '12 at 21:47
  • 1
    By instance variable, I mean object property. You do know what a @property is, correct? – Rob Napier Aug 01 '12 at 22:11
  • i do have the property o firstLocationUpdate set to non atomic and retain... and its declared in the interface... but I just did that thinking it would help retain it... but it becomes null anyway. but no, i don't know what @property does... (I'm really new to objective C and strong typed languages in general) – stackOverFlew Aug 01 '12 at 22:19
1
- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:2.0];
    [locationManager startUpdatingLocation];
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(useLocation) userInfo:nil repeats:YES];
}

-(void)useLocation{
    NSLog(@"%@",[locationManager location]);
}

You Do not actually have to implement any code in the delegate method. the [locationManager location] method will return your current location, and setting the distanceFilter will update your location if you move (2) meters (is the on board GPS that good?). So, as soon as your location changes, the location property will update automatically. This is how I typically implement it.

REFERENCE: iOS Programming: The Big Nerd Ranch Guide 3rd Edition, Chapter 4

emran
  • 902
  • 6
  • 12
  • thanks! the thing is is that I need to know the exact time between each update and I need the cllocation to update at least 10 seconds apart.... do you know if theres any other way to do that? – stackOverFlew Aug 01 '12 at 04:23
  • 1
    not exactly sure if that's possible. My best guess is that you can start updating at the beginning of the timer, then stop updating at the completion of the timer. That way the timestamp on the locationManager.location will likely change (which is what I think you're goal is). However, I do not believe this is acceptable. sorry for the delay. – emran Aug 01 '12 at 19:52
  • hmm... yeah thats what my original code was doing, but it's flawed... I'm trying right now to store a location with its timestamp (date) and then check the new ones coming in (new location) against the one i store... once the difference is 10 seconds then I can run the methods i need to run. however, I'm having trouble storing it! https://pastee.org/3umgh do you know why that's so by any chance please? – stackOverFlew Aug 01 '12 at 21:56