didEnterRegion为我的所有地理围栏开火

Mag*_*ear 12 core-location cllocationmanager cllocation swift

当GPS进入一个确定的区域时,我的所有地理围栏都会触发,起初我认为这是因为半径,但是即使在减半之后我也遇到了同样的问题.

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

    }

}
Run Code Online (Sandbox Code Playgroud)

使用(0.497337,0.575069)我只期望触发GRASS围栏,这不会发生.

输出:

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
Run Code Online (Sandbox Code Playgroud)
?    
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

虽然即使在1米处也不应该是一个问题:

小数点后四位值最多可达11米

小数点后五位值可达1.1米

小数点后六位值最大值为0.11米

ha1*_*100 5

GPS芯片和kCLLocationAccuracyBestForNavigation的最佳精度通常只有10米.

Apple表示(在位置和地图PG中)区域的最小距离应假设为200米

正如这个答案所指出的那样 - 它会有所帮助,但不能取悦你

/sf/answers/1675208671/