【问题标题】:Having issue with CLLocationManager's didEnterRegion与 CLLocationManager didEnterRegion 有问题
【发布时间】:2020-06-02 06:34:23
【问题描述】:

我正在尝试检测用户是否靠近或进入了我想要的位置。我正在使用CLLocationManager 来监视此操作,但由于某种原因它不起作用。我在当前位置,但未调用 didEnterRegion。这是我的代码:

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()


        let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
               if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
                   // Register the region.
                   let maxDistance = locationManager.maximumRegionMonitoringDistance
                   let region = CLCircularRegion(center: center,
                        radius: maxDistance, identifier: "identifier")
                   region.notifyOnEntry = true
                   region.notifyOnExit = false

                   locationManager.startMonitoring(for: region)

            }
    }


  func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    if region is CLCircularRegion {
            print("entered....")
        }
    }

我还在 plist 文件中添加了隐私描述:

我该如何解决这个问题?

已编辑:

requestState(for region:) 和 didDetermineState:for: 现在可以工作,但我如何检查用户是否在附近或在该位置?例如,如果用户在该区域附近或在该区域内,则做某事,如果不做其他事情。

已编辑 2

let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
            let region = CLCircularRegion(center: center, radius: 1000, identifier: "Store")
            let controller = LocationController()

               controller.requestWhenInUseAuthorization()

               controller.requestState(for: region) { (state) in

                   switch state {
               case .inside:
                   print("inside region")
               case .outside:
                   print("outside region")
               case .unknown:
                   print("unknown")

                   }

               }

【问题讨论】:

  • 开始监控时是否已经在区域内?
  • @Paulw11 是的.....
  • Boundary crossing notifications are delivered to your location manager's delegate object. Specifically, the location manager calls the locationManager(_:didEnterRegion:) or locationManager(_:didExitRegion:) methods of its delegate. 正如 Paulw11 所说,这些委托方法专门用于越界
  • 我可能不明白你的问题,但是在你收到进入区域的通知后,你不能简单地计算当前位置到指定位置的距离为let distance = locationManager.location.distance(from center)吗?

标签: ios swift core-location cllocationmanager


【解决方案1】:

对于一次性检查,requestState(for:) 是您想要的。

CoreLocation 将确定状态,然后在您的委托上调用 locationManager(_:didDetermineState:for:) 函数,为您提供一个 CLRegionState 枚举,您可以测试该枚举是否为 == .inside

例子:

import CoreLocation

class LocationController: CLLocationManagerDelegate {
    typealias RegionMonitoringClosure = (CLRegion, Bool) -> Void
    typealias RegionStateClosure = (CLRegion, CLRegionState) -> Void

    private let manager = CLLocationManager()
    private let stateClosure: RegionStateClosure
    private let monitoringClosure: RegionMonitoringClosure

    init(stateClosure: RegionStateClosure, monitoringClosure: RegionMonitoringClosure) {
        manager.delegate = self
        self.stateClosure = stateClosure
        self.monitoringClosure = monitoringClosure
    }

    func addMonitoredRegion(_ region: CLRegion) {
        manager.startMonitoring(for: region)
    }

    func remove(monitoredRegion region: CLRegion) {
        manager.stopMonitoring(for: region)
    }

    func requestState(for region: CLRegion) {
        manager.requestState(for: region)
    }

    // MARK: CLLocationManagerDelegate

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Entered \(region)")

        self.monitoringClosure(region, true)
    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Exited \(region)")

        self.monitoringClosure(region, false)
    }

    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print("Started monitoring region \(region)")
    }

    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
        print("Determined state \(state) for region \(region)")

        self.stateClosure(region, state)
    }
}

用法:

let regionToTest: CLRegion = ... // your desired region.
let controller = LocationController(stateClosure: { region, state in 
    switch state {
    case .inside:
        print("inside region \(region)")
    case .outside:
        print("outside region \(region)")
    }
}, monitoringClosure: { region, entered in 
    if entered == true {
       print("Entered \(region)")
       //...
    }
})

controller.requestState(for: regionToTest)

【讨论】:

  • 谢谢,抱歉回复晚了。我厌倦了使用您的课程,但是当我在设备上运行该应用程序时,它根本不起作用。我的意思是我没有得到任何日志。请检查我编辑的代码好吗?
  • @Mc.Lover 嗯。我再次更新了代码。现在可以试试了吗?
  • 抱歉,我没有收到任何日志表明我在该区域内或外。
  • @Mc.Lover 奇怪!
【解决方案2】:

如果您在模拟器上运行它,您会看到与在设备上运行它不同的结果 例如,如果您已经在您定义的区域内,并且您使用模拟器进行测试。你可能并不总是得到想要的结果。出于某种原因,如果您在打开它时要检测的区域内,模拟器不喜欢它。

此外,在我自己使用信标进行的许多测试之后,事实证明,在模拟器上与该区域的距离并不准确。使用模拟器时经常出错。它还受到您的计算机和信标之间的任何物理干扰的高度影响。该地区的许多其他设备和金属会严重影响您的模拟器报告正确数据。 我建议在物理设备上测试所有代码。 当您使用应用程序加载设备时。从您的计算机上拔下设备并打开信标区域外的应用程序。

为了进一步和更好的测试,您可以在应用程序上创建自己的本地“调试器”,以便在使用设备时无需连接到计算机即可看到所需的信息。

【讨论】:

    【解决方案3】:

    改变

      locationManager.requestWhenInUseAuthorization()
    

    到

      locationManager.requestAlwaysAuthorization()
    

    同时检查您的 gpx 文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-03
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      相关资源
      最近更新 更多