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

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

本文介绍了重要的位置变化至少每15分钟不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据苹果文档,重要的位置变化应该至少每15分钟更新一次位置。我确实在移动时收到更新,但在设备静止时却没有。您对更新的体验是什么?他们至少每15分钟来一次吗?

According to apple docs, significant location change should update location at least every 15min. I am indeed receiving updates when I move significantly, but not when the device is stationary. What is your experience with updates? Do they come at least every 15min?


推荐答案

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

Well, I have a naive solution. You can use a NSTimer to force the CLLocationManger instance to update the current location every 15 minutes or whatever the time you want it to be periodically updated.

这是我想要的代码使用:

Here's the code I would use:

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

First, call this method to start update your location whenever you need in your viewDidLoad or else where.

- (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方法:

Second, implement the updateUserLocation method:

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

最后,确认协议,然后执行location did update方法。我们阅读了最新的更新结果,并让位置经理停止更新当前位置,直到下一个15分钟。:

Last, confirm to the protocol, then implement the location did update method. We read the latest updated result and let the location manager to stop updating current location until next 15 minutes.:

-(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];
}

这篇关于重要的位置变化至少每15分钟不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:21