Ich versuche Geofencing arbeiten in CoreLocation aber obwohl ich direkt über dem Lat/Long-Punkt stehen, wird DidEnterRegion nie aufgerufen.CLLocationManager Geofencing funktioniert nicht
Hier ist mein Code:
override func viewDidAppear(_ animated: Bool) {
myLocationManager = CLLocationManager()
myLocationManager.delegate = self
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.authorizationStatus() == .notDetermined {
myLocationManager.requestAlwaysAuthorization()
} else if CLLocationManager.authorizationStatus() == .authorizedAlways {
let geoSet = arrLocSets[2]
let regionLocation: CLLocation = CLLocation(latitude: geoSet[0], longitude: geoSet[1])
let region = CLCircularRegion(center: regionLocation.coordinate, radius: 30, identifier: "region1")
region.notifyOnEntry = true
region.notifyOnExit = false
myLocationManager.startMonitoring(for: region)
print(myLocationManager.monitoredRegions)
}
if CLLocationManager.locationServicesEnabled() {
myLocationManager.startUpdatingLocation()
}
}
die Druckanrufprotokolle diese:
CLCircularRegion (identifier:'region1', center:<+40.20675900,-75.48613300>, radius:30.00m)]
was richtig ist.
der Druck Anruf in
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("started monitoring!")
}
druckt in das Protokoll so weiß ich zumindest die Überwachung gestartet.
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print(region)
/*
if let region = region as? CLCircularRegion {
let myAlert = UIAlertController(title: "Entered Region", message: "You found the region \(region.identifier)", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
myAlert.addAction(okAction)
present(myAlert, animated: true, completion: nil)
}
*/
}
und hier ist eine Bildschirmaufnahme von meinem Handy, wo ich Lat/Long verfolgt. (Ignoriere die unteren Beschriftungen, das sind vorher fest codierte Koordinaten).
Ich habe einen Breakpoint in didEnterRegion gesetzt und es wird nie ausgelöst. 206851 und 206759 sollten innerhalb eines 30M Durchmessers voneinander sein? Nein? Habe ich einen Einrichtungsschritt vermisst?
Danke Alex, ich habe gesehen, dass Apple empfiehlt mindestens 200 Meter für die DidEnterRegion zu arbeiten. Aber selbst dann bekam ich keine Ergebnisse. Dieser Code tut genau das, was ich tun muss: https://stackoverflow.com/questions/46978966/cllocation-distancefromlocation-works-better-than-checking-a-region muss nur sicherstellen, dass es skalieren kann . Die App wird den Standort nicht ständig prüfen, der Benutzer kann sie aktivieren, wenn sie sich in der Nähe des Punktes befinden, und wenn der Punkt erreicht ist, können sie ihn ausschalten. – PruitIgoe