ialameh
ialameh

Reputation: 61

CLLocationManager is giving old location

In my header i put

CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocationManager *locationManager;  

in my implementation file:

- (void)viewDidLoad
{
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.distanceFilter = 50.0f;
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    self.locationManager.delegate = self; 
    [locationManager startUpdatingLocation];
    [super viewDidLoad];
}

then in the delegate method

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
        NSLog(@"Location: %g,%g", newLocation.coordinate.latitude, newLocation.coordinate.longitude );

}

now I keep getting an old value for latitude and longitude

googled for a solution i could not find any, i don't understand what i am doing wrong,

Any help please

Upvotes: 2

Views: 1353

Answers (2)

Chazbot
Chazbot

Reputation: 1890

From my experience, the location manager will only receive a location update when the phone is moving (presumably to conserve battery life).

In other words, if the phone received a location update 20 minutes ago at a certain location, and the phone hasn't moved, when you start the location services it will return that old value as an "acceptable" value for the newLocation object.

This is why albertamg's suggestion is commonly applied, though the downside is that by filtering out these "old" updates you run the risk of not detecting anything from a motionless phone.

Hope this helps =)

Upvotes: 2

albertamg
albertamg

Reputation: 28572

You can use the timestamp of the location to filter location updates that are too old:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if ([newLocation.timestamp timeIntervalSinceNow] < -60) return; // location is not fresh
    ...
}

Upvotes: 4

Related Questions