Подтвердить что ты не робот

DidEnterRegion, стреляющий во все мои геозоны

Все мои геофункции запускаются, когда 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")
        }

    }

}

Используя (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

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

Хотя даже на 1 м это не должно было быть проблемой:

Четвертое десятичное место стоит до 11 м

Пятое десятичное место стоит до 1,1 м

Шестое десятичное место стоит до 0,11 м

4b9b3361

Ответ 1

Наилучшая точность с чипом GPS и kCLLocationAccuracyBestForNavigation часто составляет всего 10 метров.

Apple говорит (в Location and Maps PG), что минимальное расстояние для регионов должно быть 200 м

как указано в этом ответе, - это поможет, но не рад вам.

fooobar.com/questions/202428/...

Ответ 2

Если кто-то поможет, кто споткнулся об этом, я больше не стал с CLRegion.

Вместо этого я пошел с классом CLLocation и использовал . distanceFromLocation и разработал расстояние для каждого из моих регионов/местоположений.