CLLocationManager didUpdateToLocation checking failing

I am trying to get the location coordinates using CLLocationManager. Here is my code

- (void)viewDidLoad {
    [super viewDidLoad];

    //instantiate location manager and set delegate
    self.locationManager=[[CLLocationManager alloc]init];
    locationManager.delegate=self;
    // can be set to 100m,1km,3km etc.
    //locationManager.distanceFilter=10.0f;
    locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
    //start updating the delegate
    [locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation{

    // Check if the coordinates are different from the old ones
    if (newLocation.coordinate.latitude != oldLocation.coordinate.latitude && 
        newLocation.coordinate.longitude != oldLocation.coordinate.longitude) {     

        NSLog(@"not equal");        
    } else {
        NSLog(@"equal");
    }
}

However i find the condition is called twice. First time the condition is satisfied and prints not equal and immediately its called again and prints “equal”. Can some1 help me out ? What am i doing wrong ?

  • iOS facebook sdk how to download album, profile photos data
  • Pushwhoosh remote notification
  • XCode - Dynamically created labels, when i change the text it changes it for the last one only
  • cocos2d iphone 5 4 inch display support
  • How to check if downloaded PNG image is corrupt?
  • iPhone - Remove status bar programmatically
  • Thanks

    Solutions Collect From Internet About “CLLocationManager didUpdateToLocation checking failing”

    This is because CoreLocation cache your last location and return it immediately after you called startUpdatingLocation so you have to validate coordinate timestamp and it is too old, you can ignore old coordinate.

    UPDATE:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        if([newLocation horizontalAccuracy] < 0.0f) return;
        if(fabs([[newLocation timestamp] timeIntervalSinceNow]) > kCLLocationMaximumLocationDataAge) return;
        if(fabs([[oldLocation timestamp] timeIntervalSinceNow]) < kCLLocationMaximumLocationDataAge && [newLocation getDistanceFrom:oldLocation] < 0.1f && [newLocation horizontalAccuracy] == [oldLocation horizontalAccuracy])
            return;
        if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
            NSLog(@"---> \n%@\n%@\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
        } else {
            NSLog(@"\n-------------- BAD ---------------\n%@\n%@\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs\n----------------------------------", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
        }
        if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
            [self stopLocationImprovementTimer];
        } else [self createLocatinoImprovementTimer];
    }
    

    createLocatinoImprovementTimer method used to launch timer, which will work for certain amount of time and if it wasn’t stopped it will send and update notification with location, which was last. This timer will help to wait for coordinate with better accuracy before notify controllers.