1

I initialize the locationManager this way:

if (!self.locManager) 
{
    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    [locManager startMonitoringSignificantLocationChanges];
}

my device is not moving and still "didUpdateToLocation" is being called every time. What could be a problem? Thanks

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
Alex Guberman
  • 203
  • 1
  • 3
  • 15

2 Answers2

3

didUpdateToLocation may update for a number of reasons, a good strategy for handling this is to gradually filter results based on timestamp, then required accuracy.

Apple provide a good example in the LocateMe sample app:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;

    // test the measurement to see if it is more accurate than the previous measurement
    if (self.bestEffortAtLocation == nil || self.bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy)
    {
        // store the location as the "best effort"
        self.bestEffortAtLocation = newLocation;

        // test the measurement to see if it meets the desired accuracy
        //
        // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
        // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
        // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
        //
        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
            // we have a measurement that meets our requirements, so we can stop updating the location
            // 
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            //
            [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];
        }
    }
}
cleverbit
  • 5,514
  • 5
  • 28
  • 38
  • Thanks, but what if I want the location services to stay opened? stopUpdating probably stops the location manager no? – Dejell Sep 11 '13 at 16:13
  • This code allows the Location manager to update as often as necessary to pinpoint the user to the desired accuracy. At which point, it is courtesy and sensible for power management to stop updating. If your app needs to monitor the User as they change locations, then you can register for significant-change: `[locationManager startMonitoringSignificantLocationChanges];` See [docs](https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html) – cleverbit Sep 12 '13 at 14:07
0

Do you check for location differences? CoreLocation also calls callbacks when other attributes such as accuracy, heading or speed change

startMonitoringSignificantLocationChangesshould give you an initial fix and after that you will get callbacks for "significant changes" (cell tower change etc.)

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217