Search code examples
ioscllocation

Periodic background location - using desired accuracy and distance filter


I am trying to use this technique in iOS7 to get a location every x seconds - Periodic iOS background location updates.

In my didUpdateLocations:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
    [self.locationManager setDistanceFilter:9999];
}

When i run this, i seem to get a loop. setDesiredAccuracy: - i think this forces an update itself, on change. This is why I am getting a loop, would this be right? When I comment these out, it acts like I expect it too. Is there anyway to stop it get location when i set these?


Solution

  • Sounds like you should set some kind of time/date stamp on how often you call "setDesiredAccuracy". If you call it once, don't call it again for some certain period of time.

    something like:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        NSTimeInterval secondsSinceLastResetOfAccuracy = [lastResetTime timeIntervalSinceNow];
        if(secondsSinceLastResetOfAccuracy > 60 * 60) // one hour
        {    
            [self.locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
            [self.locationManager setDistanceFilter:9999];
            lastResetTime = [NSDate date]; // new "time stamp"
        }
    }