ios - iOS 每 x 秒更新一次位置

标签 ios objective-c location

我正在尝试构建一个 iOS,我可以在其中每 x 秒更新一次我的位置并发送通知以更新 UI。 我得到了我的位置,但更新是随机的。任何想法如何添加间隔?

这是我的代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    [self sendNotification :@"long"
                           :[NSString stringWithFormat:@"%.8f",location.coordinate.longitude]];
    [self sendNotification :@"lat"
                           :[NSString stringWithFormat:@"%.8f",location.coordinate.latitude]];

}

最佳答案

试试这个:

在.h文件中声明

@property (strong, nonatomic) NSDate *lastTimestamp;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *mostRecentLocation = locations.lastObject;
    NSLog(@"Current location: %@ %@", @(mostRecentLocation.coordinate.latitude), @(mostRecentLocation.coordinate.longitude));

    NSDate *now = [NSDate date];
    NSTimeInterval interval = self.lastTimestamp ? [now timeIntervalSinceDate:self.lastTimestamp] : 0;

    if (!self.lastTimestamp || interval >= 5 * 60)
    {
        self.lastTimestamp = now;
        NSLog(@"update your UI");
    }
}

关于ios - iOS 每 x 秒更新一次位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43537723/

相关文章:

ios - 如何防止 Swift 中的对象构造

iphone - 防止文件备份到 iCloud

ios - HMAccessoryDe​​legates 未调用按钮操作

objective-c - 在 swift 类中使用来自 objective-c 的枚举

android - 从 Location 对象获取卫星数量

ios - SpriteKit 滚动底部在循环时有间隙

ios - 自动布局的优点和缺点

ios - 在后台运行代码并获取返回码

python-3.x - python : How to extract addresses from a sentence/paragraph (non-Regex approach)?

android,无需遍历所有位置即可找到最近的位置