代码之家  ›  专栏  ›  技术社区  ›  CodeReaper

如何在iOS 11上避免gps跟踪差距

  •  3
  • CodeReaper  · 技术社区  · 7 年前

    我在跟踪应用程序中遇到问题,但仅在iOS 11上。在某些情况下,应用程序会在后台被动记录您的GPS位置。

    iOS 11上出现的问题是,CLLocationManager似乎随机停止报告GPS事件10到900秒以上。

        let locationManager = CLLocationManager()
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.startMonitoringSignificantLocationChanges()
        locationManager.desiredAccuracy = 10
        locationManager.activityType = .automotiveNavigation
    

    如果认为线程核心位置正在管理和使用所有回调,可能会负担过重。

        let queue = OperationQueue()
        queue.maxConcurrentOperationCount = 1
        queue.qualityOfService = .userInitiated
    

    通过使用该操作队列的回调:

        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            queue.addOperation {
                // process locations
            }
        }
    

    引入操作队列并没有帮助避免间隙,但确实做到了这一点,因此当间隙出现时,位置管理器会报告一组具有相同时间戳的(不同)位置。

    具有相同时间戳的位置并不是所有缺失的位置,即如果有200秒的间隔,我可能只会得到15个具有相同时间戳的位置。

    我希望在座的人能告诉我为什么会发生这种情况,以及我能做些什么来避免这些差距。

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   CodeReaper    6 年前

    经过与苹果公司的多次审查、试验和讨论,我们现在似乎已经解决了这个问题。即使苹果只保证GPS跟踪背景,如果应用程序在跟踪开始时位于前台。我们应用的修复正在更改以下内容:

            locationManager.startMonitoringSignificantLocationChanges()
    

    ... 收件人:

            locationManager.stopMonitoringSignificantLocationChanges()
            locationManager.startMonitoringSignificantLocationChanges()
    

    理论上,当在启动期间将true应用于“监控”设置时,应用程序的设置会被破坏。如果首先将“监控”设置设置为false,然后将其设置为true,则不会发生损坏。