我正在构建GPS应用程序,并且已经能够成功获取位置更新。但是,这些更新正在快速进行(约1-2秒)。我希望能够每15秒更新一次UILabel。我的代码有什么问题?
附言[self setLabels:manager];
只是将UILabels设置为managers属性的值:manager.location.coordinate.latitude
和longitude:manager.location.coordinate.longitude
谢谢!
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
//SHOULD BE LOOPING INTO HERE EVERY 15 SECONDS. INSTEAD IT IS 1-2 SECONDS
[self setLabels:manager];
}
}
最佳答案
创建一个像这样的iVar:
@property (nonatomic, strong) NSDate *lastUpdatedTime;
然后只需检查
lastUpdatedTime
是否已超过15秒(或第一次为零),并在每次设置标签时更新日期。- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if( [self.lastUpdatedTime timeIntervalSinceNow] > 15 || self.lastUpdatedTime == nil ){
[self setLabels:manager];
self.lastUpdatedTime = [NSDate date];
}
}
这样,您可以保持GPS更新的假设状态。但只能每15秒更新一次标签。
关于ios - 每15秒更新一次位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22008302/