【问题标题】:significant location change does not trigger at least every 15min至少每 15 分钟不会触发重大位置变化
【发布时间】:2016-05-30 16:48:42
【问题描述】:

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

如果 GPS 级别的准确性对您的应用来说并不重要并且您不需要 持续跟踪,您可以使用显着变化的位置 服务。使用显着更改位置至关重要 服务正确,因为它至少唤醒了系统和您的应用程序 每 15 分钟一次,即使没有发生位置变化,并且它 持续运行直到你停止它。

【问题讨论】:

    标签: ios location core-location cllocationmanager


    【解决方案1】:

    好吧,我有一个幼稚的解决方案。您可以使用 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];
    }
    

    【讨论】:

    • 不确定计时器是否在后台工作。仍然是一个不错的方法
    猜你喜欢
    • 2016-05-29
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2017-07-08
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    相关资源
    最近更新 更多