【问题标题】:How can I check if user is in the correct region by pressing a button? Swift如何通过按下按钮检查用户是否在正确的区域?迅速
【发布时间】:2019-02-22 00:19:23
【问题描述】:

我正在使用Core LocationMonitoring 用户邻近度。我的监控运行良好,但我想检查用户是否按下按钮checkIn,检查他是否在正确的位置。我正在考虑将按钮连接到我的didEnterRegion 函数,并能够提示一条消息,告诉用户你在这个地方。这是我的监控代码和didEnterRegion

    @IBAction func checkIn(_ sender: UIButton) {

     if inSidePlace == true {
        print("You are in the correct place")
    } else {
        print("You are not in the correct place")
    }

}

    func monitorRegionAtLocation(center: CLLocationCoordinate2D, identifier: String) {

    //Here we need to check if is always or only when Using app.... doesn't make sense when using app for remote notifications if CLLocationManager.authorizationStatus() == .authorizedAlways {
        if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
            // Register the region.
            let region = CLCircularRegion(center: center,
                                          radius: 20, identifier: identifier)
            region.notifyOnExit = true
            region.notifyOnEntry = true

            let circle = MKCircle(center: center, radius: region.radius)
            mapView.add(circle)
            locationManager.startMonitoring(for: region)
    }
  }
}
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

    if let region = region as? CLCircularRegion {
        let identifier = region.identifier
        print("You are IN: " + identifier)

        let title = "You entered the region!"
        let message = "You are in the correct place!"
        showAlert(title: title, message: message )
        showNotification(title: title, message: message)
        //If is possible to connect the button here so when user pressed it we can at least show the identifier
    }
}

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

    if let region = region as? CLCircularRegion {
        let identifier = region.identifier
        print("You are NOT: " + identifier)

        let title = "You left the region!"
        let message = "You are not in the correct place!"
        showAlert(title: title, message: message)
        showNotification(title: title, message: message)


    }
}

任何额外的信息让我知道。谢谢

【问题讨论】:

    标签: swift function button core-location


    【解决方案1】:

    也许您可能有一个简单的实例变量来跟踪此人是否已进入该区域?

    //Your instance var:
    var isInsideRegion = false
    
    
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    
        if let region = region as? CLCircularRegion {
            self.isInsideRegion = true  //update the instance variable here
            let identifier = region.identifier
        }
    }
    
    @IBAction func checkIn(_ sender: UIButton) {
    
    if self.isInsideRegion {
        let title = "You entered the region!"
        let message = "You are in the correct place!"
        showAlert(title: title, message: message )
        showNotification(title: title, message: message)
    }
    

    }

    【讨论】:

    • 我只是在我的代码中使用if 语句编辑动作button 并且完美运行唯一的问题是如果用户已经在该区域中检查为false,有任何检查用户是否已经在设置为true 的位置以及如何将identifier 名称传递给我的按钮操作的方法,我尝试使用全局var 并且不起作用!
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2023-03-19
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多