views:

177

answers:

1

I was wondering if anyone could point me to (or paste in) some code to deal with turning off Core Location updates to save power.

As far as I understand it, you should stop Core Location updates as soon as you get a reading of desired accuracy. If you don't get a good accuracy reading after a certain time, you should also stop updates (presumably using a timer). Every time you stop updates, you should fire a timer (around 60 seconds) to restart Core Location and get a new reading.

Is there Apple code which does all this? The LocateMe, TaggedLocations and Locations sample code don't seem to do it.

+2  A: 

The LocateMe example has the code you need. You just need to create a second Selector to Fire. LocateMe calls the following in it's setup method...

    [self performSelector:@selector(stopUpdatingLocation:) withObject:@"Timed Out" afterDelay:[[setupInfo objectForKey:kSetupInfoKeyTimeout] doubleValue]];

It says that after a certain amount of time (kSetupInfoKeyTimeout), please call the stopUpdatingLocation method the the argument of NSString = "Timed Out". Inside the stopUpdatingLocation method, the [locationManager stopUpdatingLocation] is called to tell CoreLocation to stop.

So, all you need to do is add another Selector like this...

[self performSelector:@selector(timeToRestartCoreLocation) afterDelay: 60];

inside the stopUpdatingLocation method, which will call the timeToRestartCoreLocation method after 60 seconds. Then inside your timeToRestartCoreLocation method, call [locationManager startUpdatingLocation] to kick off CoreLocation again.

ThePaddedCell
I'm looking through that code now and it has a lot of what I want. The timer's aren't exactly what I need, but enough to get me started. Thanks!
nevan
You're welcome :)
ThePaddedCell