2

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

1
  • Is this on the device or the simulator?
    – Black Frog
    Jun 5, 2011 at 18:58

2 Answers 2

4

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
    ...
}
2
  • I tried to comment the distanceFilter, however still getting the same
    – ialameh
    Jun 5, 2011 at 18:20
  • One more thing to note is that maps application on iphone shows the exact current location
    – ialameh
    Jun 5, 2011 at 18:36
2

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 =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.