开发者

CLLocationManager is giving old location

In my header i put

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

in my implementation file:

- (void)viewDidLoad
{
    self.locat开发者_C百科ionManager = [[[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


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
    ...
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜