CLLocationManager地理围栏无法正常工作

时间:2017-10-26 15:48:49

标签: cllocationmanager

我正在尝试在CoreLocation中使用geofencing,但即使我站在lat / long点的顶部,didEnterRegion永远不会被调用。

这是我的代码:

override func viewDidAppear(_ animated: Bool) {

    myLocationManager = CLLocationManager()
    myLocationManager.delegate = self
    myLocationManager.desiredAccuracy = kCLLocationAccuracyBest

    if CLLocationManager.authorizationStatus() == .notDetermined {
        myLocationManager.requestAlwaysAuthorization()
    } else if CLLocationManager.authorizationStatus() == .authorizedAlways {

        let geoSet = arrLocSets[2]
        let regionLocation: CLLocation = CLLocation(latitude: geoSet[0], longitude: geoSet[1])
        let region = CLCircularRegion(center: regionLocation.coordinate, radius: 30, identifier: "region1")
        region.notifyOnEntry = true
        region.notifyOnExit = false

        myLocationManager.startMonitoring(for: region)

        print(myLocationManager.monitoredRegions)
    }

    if CLLocationManager.locationServicesEnabled() {
        myLocationManager.startUpdatingLocation()
    }

}

打印呼叫记录此信息:

CLCircularRegion (identifier:'region1', center:<+40.20675900,-75.48613300>, radius:30.00m)]

这是正确的。

中的打印电话
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print("started monitoring!")
    }

打印到日志,所以我知道至少监控已开始。

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

    print(region)
    /*
    if let region = region as? CLCircularRegion {

        let myAlert = UIAlertController(title: "Entered Region", message: "You found the region \(region.identifier)", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        myAlert.addAction(okAction)
        present(myAlert, animated: true, completion: nil)
    }
     */
}

这是我手机上的屏幕截图,我跟踪的是lat / long。 (忽略底部标签,这些是以前的硬编码坐标)。

我在didEnterRegion中设置了一个断点,它永远不会被触发。 206851和206759之间的直径应该在30M之内吗?没有?我错过了设置步骤吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

didEnterRegion / didExitRegion仅在状态更改时发生。当在区域内开始监视区域时,您将不会收到didEnterRegion。

开始走出去,直到didExitRegion发生,这将“重新安置”didEnterRegion事件的区域,现在你可以重新进入。

不要惊讶于你必须在每个方向走超过30米来触发事件。准备长达300米的旅程 - 真的取决于您的速度和(无线)环境。区域监控根本不涉及GPS,它完全是关于wifi /蜂窝塔三角测量 - 取决于覆盖范围,先前测量的质量而不是“实时”,因为它是由wifi /小区扫描驱动的。