didEnterRegion firing for all my geofences
Asked Answered
A

2

12

All my geofences are triggering when GPS enters a defined region, at first I thought it was because of the radius, however even after halving it I am having the same problem.

import UIKit
import CoreLocation


class itemDesc {
    var title: String
    var coordinate: CLLocationCoordinate2D
    var regionRadius: CLLocationDistance
    var location: String
    var type: String

    init(title: String, coordinate: CLLocationCoordinate2D, regionRadius: CLLocationDistance, location: String, type: String) {
        self.title = title
        self.coordinate = coordinate
        self.regionRadius = regionRadius
        self.location =  location
        self.type = type
    }

}

class ViewController:  UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {

        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        setupData()
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

    }

    func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
        print("Monitoring failed for region with identifier: \(region!.identifier)")
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Location Manager failed with the following error: \(error)")
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }

    func handleRegionEvent(region: CLRegion!) {
        print("Geofence triggered \(region.identifier)")
    }

    func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
        if region is CLCircularRegion {
            handleRegionEvent(region)
        }
    }

    func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
        if region is CLCircularRegion {

        }
    }

    func setupData(){
        if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) {

            let itemRegion = [
                itemDesc( title: "DOOR", coordinate: CLLocationCoordinate2DMake(00.497699, 00.575095), regionRadius: 0.5, location: "DOOR", type: "exterior"),
                itemDesc( title: "BARN FRONT", coordinate: CLLocationCoordinate2DMake(00.49751, 00.575149), regionRadius: 0.5, location:"BARN FRONT", type: "exterior"),
                itemDesc( title: "GRASS", coordinate: CLLocationCoordinate2DMake(00.497337, 00.575069), regionRadius: 0.5, location: "GRASS ", type: "nature")]

            for item in itemRegion {

                let coordinate = item.coordinate
                let regionRadius = item.regionRadius
                let title = item.title
                let region = CLCircularRegion(center: coordinate, radius: regionRadius, identifier: title)

                locationManager.startMonitoringForRegion(region)

            }
        } else{
            print("system can't track regions")
        }

    }

}

Using (0.497337, 0.575069) I'd only expect the GRASS fence to be triggered, this is not happening.

Outputs:

regionRadius = 1.0

locations = 37.33233141 -122.0312186
locations = 37.33233141 -122.0312186
locations = 0.497337 0.575069
Geofence triggered BARN FRONT
Geofence triggered DOOR
Geofence triggered GRASS

regionRadius = 0.5

locations = 37.33233141 -122.0312186
locations = 37.33233141 -122.0312186
locations = 0.497337 0.575069
Geofence triggered BARN FRONT
Geofence triggered DOOR
Geofence triggered GRASS

Although even at 1m this should not have been a problem:

The fourth decimal place is worth up to 11 m

The fifth decimal place is worth up to 1.1 m

The sixth decimal place is worth up to 0.11 m

Allyn answered 11/8, 2016 at 3:56 Comment(0)
G
5

The best accuracy with the GPS chip and kCLLocationAccuracyBestForNavigation is often just 10 meters.

Apple says (in the Location & Maps PG) that the minimum distance for regions should be assumed to be 200m

as pointed out by this answer - it will help but not please you

https://mcmap.net/q/466113/-ios-geofence-clcircularregion-monitoring-locationmanager-didexitregion-does-not-seem-to-work-as-expected

Goulden answered 13/8, 2016 at 13:39 Comment(0)
A
1

If it helps anybody out who stumbles across this, I didn't get any further with CLRegion.

Instead, I went with CLLocation class and use .distanceFromLocation and worked out the distance for each of my regions/locations.

Allyn answered 18/8, 2016 at 23:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.