[英]Swift 4 locationManager didEnterRegion doesn't fire when user is in region on app launch
我試圖檢測,如果用戶正在我的應用程序中輸入特定區域。
如果用戶在應用程序啟動后不在特定區域,然后進入該區域,則此方法可以正常工作。
但是,如果用戶在應用程序啟動時位於特定區域,則不會觸發“didEnterRegion”功能。
這是我的代碼:
override func viewDidLoad() {
super.viewDidLoad()
self.dbRef = Database.database().reference()
self.mapView.delegate = self
self.initLocationManager()
guard let userLocation = locationManager.location?.coordinate else {return}
self.userLocation = userLocation
let region = MKCoordinateRegion(center: userLocation, latitudinalMeters: 1200, longitudinalMeters: 1200)
mapView.setRegion(region, animated: true)
self.getPlaces { (places) in
self.places = places
self.addAnnotationsToMap(places)
if(CLLocationManager.authorizationStatus() == .authorizedAlways){
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self){
for place in places{
self.startMonitoring(place)
}
}
}
}
}
func startMonitoring(_ place: Place){
let region = CLCircularRegion(center: place.getCoordinate(), radius: 50, identifier: place.identifier)
region.notifyOnEntry = true
region.notifyOnExit = true
locationManager.startMonitoring(for: region)
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entered Region : \(region.identifier)")
}
這是預期的行為,顧名思義,它僅在更廣泛的交叉區域觸發。 如果要確定用戶是在區域內還是在區域外,請使用locationManager.requestState(for region: CLRegion)
然后在委托函數locationManager(_:didDetermineState:for:)
接收結果locationManager(_:didDetermineState:for:)
要查詢所有受監視區域,請為locationManager.monitoredRegions
每個對象調用requestState(region)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.