[英]How can I check if user is in the correct region by pressing a button? Swift
我正在使用“ Core Location
並Monitoring
用戶接近程度。 我的監視效果很好,但是我想檢查用戶是否按下了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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.