简体   繁体   English

CLLocationManager 提供旧位置

[英]CLLocationManager is giving old location

In my header i put在我的 header 我把

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请提供任何帮助

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.换句话说,如果手机在 20 分钟前在某个位置接收到位置更新,并且手机没有移动,当您启动位置服务时,它将返回旧值作为新位置 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.这就是为什么 albertamg 的建议被普遍应用的原因,尽管不利的一面是,通过过滤掉这些“旧”更新,你冒着无法从静止的手机中检测到任何东西的风险。

Hope this helps =)希望这会有所帮助=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM