swift - 如何通过按下按钮来检查用户是否位于正确的区域? swift

标签 swift function button core-location

我正在使用核心位置监控用户接近度。我的监控工作正常,但我想检查用户是否按下按钮 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)


    }
}

任何额外信息请告诉我。谢谢

最佳答案

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

//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)
}

}

关于swift - 如何通过按下按钮来检查用户是否位于正确的区域? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52378501/

相关文章:

swift - 为什么可以在 Swift 中将非可选值分配给可选类型?

postgresql - 如何在 PostgreSQL 中创建一个新函数作为另一个函数的别名?

swift - 在 init() 中使用 guard let 安全解包可选

javascript - HTML Javascript,.click()->.submit() 和 .submit() 返回不同的结果

vba - Excel VBA : Formula is too complex for object

ios - 我希望我的 View Controller 具有相同的自定义 UIView 组合,它们可以相互通信

swift - 从 nib 实例化 UIView 导致无限循环问题

css - 如何点击外部按钮并保持事件样式? - AlpineJS 和 TailwindCSS

ios - iOS 应用程序中谷歌地图上的实时交通状况?

function - 对于良好的日常习惯来说,哪一个特征最重要?