gpt4 book ai didi

ios - 核心位置会在背景出现时给出旧位置

转载 作者:行者123 更新时间:2023-12-01 16:59:17 25 4
gpt4 key购买 nike

我在VC中有一个CoreLocation管理器,当用户按下“获取方向”按钮时,我启动了位置管理器,然后应用打开了带有当前位置和一些预定义目标位置的google map方向。

这是我的问题,如果应用程序不是处于后台状态,当前位置几乎总是为true,但是如果应用程序在同一VC中从后台调用并且用户再次按下“获取方向”按钮,则当前位置通常显示旧位置。简而言之,我在进行多任务处理时遇到麻烦,并且获取位置的时间戳无法解决我的问题。

IBAction:

   if ( self.locationManager ) {
[_locationManager release];
self.locationManager = nil;
}
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;

self.locationTimer = [NSTimer scheduledTimerWithTimeInterval:LOCATION_TIMER target:self selector:@selector(stopUpdatingLocationTimer) userInfo:nil repeats:NO];
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
[self.locationManager startUpdatingLocation];

核心位置代表:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {


NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"%f",locationAge);
if (locationAge > 3.0)
return;

if (newLocation.horizontalAccuracy < 0)
return;

if ( self.currentLocation == nil || self.currentLocation.horizontalAccuracy > newLocation.horizontalAccuracy ) {

self.currentLocation = newLocation;

if (self.currentLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {

[self stopUpdatingLocations:YES];

}

}

}

最佳答案

在您的示例中,locationAge是自timestampnewLocation以来的秒数的否定表示。这意味着locationAge永远不会大于3,并且您实际上是在让每次更新都过筛。

像这样设置locationAge:

NSTimeInterval locationAge = [newLocation.timestamp timeIntervalSinceNow];

关于ios - 核心位置会在背景出现时给出旧位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com