位置管理器didExitRegion未被调用

时间:2016-04-07 20:08:25

标签: ios core-location cllocationmanager geofencing

我已经设置了委托,创建了一个区域,并且我已经阅读了所有文档。每次用户的位置更改并且超出指定区域时,都不会调用locationManager(manager: CLLocationManager, didExitRegion region: CLRegion)。我已经看到它被调用一次然后它就停止了它。我花了很多时间试图弄清楚发生了什么。

快速浏览我正在做的事情:

当应用程序启动时,我调用locationManager.startUpdatingLocation()进而调用代理locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]),我向服务器快速请求我需要的一些信息,创建CLCircularRegion并继续致电locationManager.stopUpdatingLocation()。在那种方法中,我打电话给我startMonitoringForRegion(_:)

是否可能是因为每次我创建一个新区域时它都使用相同的标识符? Apple的文档说可以使用相同的标识符,因为它将取代之前的标识符,但我想知道是否导致委托不调用didExitRegion

我的代码如下。谢谢。

  func configureLocation() {

    if CLLocationManager.locationServicesEnabled() {

        locationManager.delegate = self

        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.allowsBackgroundLocationUpdates = false
        locationManager.pausesLocationUpdatesAutomatically = true

        // Distance filter to update location
        //locationManager.distanceFilter = 10.0

        // Begin updating user location
        locationManager.startUpdatingLocation()

    } else {

        let title = "Location disabled"
        let message = "To enable location services go to Settings > Privacy > Location Services"
        self.locationServicesAlertMessage(title, message: message)
    }
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print("didUpdateCalled")
    /*
    if UIApplication.sharedApplication().applicationState == .Active {
        print("ACTIVE UPDATE: location")
    } else if UIApplication.sharedApplication().applicationState == .Background {
        print("BACKGROUND UPDATE: location")
    }
    */

    if firstLocationUpdate {
        let center = CLLocationCoordinate2D(latitude: (manager.location?.coordinate.latitude)!, longitude: (manager.location?.coordinate.longitude)!)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpanMake(0.01, 0.01))

        self.mapView.setRegion(region, animated: true)

        self.firstLocationUpdate = false
    }


    // If the user is travelling less than 5 mph, update location
    if manager.location?.speed <= 3.5 {

        self.updateLongAndLat(manager.location!) { (lat, long) in

            print("LAT: \(lat)")
            print("LONG: \(long)")
            if lat != nil && long != nil {
                let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
                let monitoredRegion = CLCircularRegion(center: center, radius: 10.0, identifier: "UserRegion")
                monitoredRegion.notifyOnEntry = false
                self.locationManager.startMonitoringForRegion(monitoredRegion)
                self.locationManager.stopUpdatingLocation()
            }
        }
    }
}

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
    print("EXITED REGION")

    print("Identifier: \(region.identifier)")
    if manager.location?.speed <= 3.5 {
        self.updateLongAndLat(manager.location!, completion: { (lat, long) in
            if lat != nil && long != nil {
                let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
                let monitoredRegion = CLCircularRegion(center: center, radius: 10.0, identifier: "UserRegion")
                self.locationManager.startMonitoringForRegion(monitoredRegion)
            }
        })
    }
}

1 个答案:

答案 0 :(得分:4)

区域监控不依赖于GPS。它是手机信号塔/ wifi接入点三角测量。你不应该期望水平精度优于65米。尝试使用65米精确仪器检测10米运动根本不起作用。

当我被要求监控半径小于100米的区域时,我不知道iOS会做什么。最好只是将半径限制为100.在这种情况下,您的应用程序仍然可以工作,但可能不是它的预期方式。

要从评论中回答您的问题,我担心Apple无法深入了解其核心定位技术。这就是开发人员之间存在很多混淆的原因,因为似乎没有像预期的那样工作。对于初学者而言,核心位置API旨在实施最佳化实践,以最大限度地减少电池消耗。

如果你想使用比300米更精确的位置服务(是的,三百),你必须使用GPS。地理围栏将无法与不断行走的用户保持同步。但是在背景中使用GPS不断运行是不行的,因为你要快速耗尽电池并且你的应用程序将很快被用户手机驱逐。

地理围栏不需要GPS,但如果可用的话,很乐意使用GPS数据(即使其他应用程序要求GPS更新)。 GPS辅助地理围栏的工作非常精确,但我认为没有任何合理的理由同时使用这两种服务,因为将位置管理器过滤器设置到正确的距离更简单。