【问题标题】:"didUpdateLocations" doesn't being called when monitoring significant location changes监视重大位置更改时不会调用“didUpdateLocations”
【发布时间】:2016-07-17 18:41:27
【问题描述】:

我正在尝试使用名为 LocationService 的单例 locationManager 监控用户的重大位置变化,它是这样的:

LocationService.Swift:

 class var sharedInstance: LocationService {
        struct Static {
            static var onceToken: dispatch_once_t = 0

            static var instance: LocationService? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = LocationService()
        }
        return Static.instance!
    }

    var locationManager: CLLocationManager?
    var location: CLLocation?
    var delegate: LocationServiceDelegate?

    override init() {
        super.init()

        self.locationManager = CLLocationManager()
        guard let locationManager = self.locationManager else {
            return
        }

        if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestAlwaysAuthorization()
        }

        locationManager.delegate = self
        locationManager.distanceFilter = 10
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.pausesLocationUpdatesAutomatically = false

        if NSString(string: UIDevice.currentDevice().systemName).floatValue >= 9 {
            locationManager.allowsBackgroundLocationUpdates = true
        }
    }

func startUpdatingLocation() {
        print("Starting Location Updates")
        self.locationManager?.startUpdatingLocation()
    }

func startMonitoringSignificantLocationChanges() {
        print("Starting Significant Location Updates")
        self.locationManager?.startMonitoringSignificantLocationChanges()
    }

// CLLocationManagerDelegate
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let location = locations.last else {
            return
        }

        // singleton for get last location
        self.location = location
    }

myVC.swift:

private let locationService = LocationService.sharedInstance

func prepareInformation() {
        self.locationService.delegate = self
        self.locationService.startMonitoringSignificantLocationChanges()
    }

但是 didUpdateLocations 在应用程序启动时只被调用一次,然后它甚至没有被调用。但是当我换行时:

self.locationService.startMonitoringSignificantLocationChanges()

到:

self.locationService.startUpdatingLocation()

效果很好,每次用户移动时都会调用。

可能是什么问题?谢谢!

【问题讨论】:

    标签: ios iphone swift cocoa-touch core-location


    【解决方案1】:

    根据苹果文档

    在返回当前位置修复后,接收器生成更新 仅当用户位置发生重大变化时才发生事件 检测到。 它不依赖 distanceFilter 属性中的值 生成事件。

    还有

    只要设备移动 500 米,应用就会收到通知 或更多来自其先前的通知。它不应该期望 通知的频率高于每五分钟一次。如果 设备能够从网络中检索数据,位置管理器 更有可能及时发送通知

    我认为您期望用户将其位置更改 10 米时发生事件,但此方法不依赖于 distanceFilter。无论您何时使用 startUpdatingLocation(),您都会收到取决于 distanceFilter 属性的事件。

    您可以阅读有关此here 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      相关资源
      最近更新 更多