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

根据苹果文档,重要的位置变化应该至少每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];
}

最后,确认协议,然后实现位置更新方法.我们阅读最新的更新结果,让位置管理员停止更新当前位置,直到下一个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];
}
点赞