CLLocationManager is giving old location

╄→гoц情女王★ 提交于 2021-01-28 04:22:13

问题


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:


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:


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



来源:https://stackoverflow.com/questions/6244649/cllocationmanager-is-giving-old-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!