重要的位置变化不会至少每 15 分钟触发一次

significant location change does not trigger at least every 15min

根据 apple 文档,重要的位置更改应至少每 15 分钟更新一次位置。当我大幅移动时,我确实会收到更新,但当设备静止时则不会。您对更新有何体验?他们至少每 15 分钟来一次吗?

If GPS-level accuracy isn’t critical for your app and you don’t need continuous tracking, you can use the significant-change location service. It’s crucial that you use the significant-change location service correctly, because it wakes the system and your app at least every 15 minutes, even if no location changes have occurred, and it runs continuously until you stop it.

好吧,我有一个天真的解决方案。您可以使用 NSTimer 强制 CLLocationManger 实例每 15 分钟或任何您希望它定期更新的时间更新当前位置。

这是我要使用的代码:

首先,调用此方法以在您需要时在您的 viewDidLoad 或其他地方开始更新您的位置。

- (void)startStandardUpdates
{
    if (nil == locationManager){
        locationManager = [[CLLocationManager alloc] init];
    }

    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // 900 seconds is equal to 15 minutes
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:900 target:self selector:@selector(updateUserLocation) userInfo:nil repeats:YES];
    [timer fire];    
}

其次,实现updateUserLocation方法:

-(void)updateUserLocation{
    [self.locationManager startUpdatingLocation];
}

最后,确认协议,然后执行location did update方法。我们读取最新的更新结果并让位置管理器在接下来的 15 分钟内停止更新当前位置。:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *userLocation = [locations objectAtIndex:0];
    CLLocationCoordinate2D userLocationCoordinate = userLocation.coordinate;
    /*
    Do whatever you want to update by using the updated userLocationCoordinate.
    */
    [manager stopUpdatingLocation];
}