我将如何使用一系列信息创建地理围栏?

How would I create geofences using an array of information?

我正在尝试按照 this tutorial 为位置设置地理围栏,但我想使用从我的 Firebase 数据库中获取的一系列信息来创建地理围栏。有谁知道我会怎么做或者有任何教程可以 link 给我吗?由于我是 Swift 的新手,所以我正在努力思考如何做到这一点。有人可以帮助解释我会做什么或指出 someone/somewhere 可以解释这个吗?

像这样:

func startMonitoring(_ manager:CLLocationManager, region:CLCircularRegion) {
    if !CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
        print("Cannot monitor location")
        return
    }
    if CLLocationManager.authorizationStatus() != .authorizedAlways {
        print("Please grant access")
    } else {
        let locationManager = CLLocationManager()
        locationManager.startMonitoring(for: region)
    }
}

func getRegionForLocation(_ location:CLLocation) -> CLCircularRegion {
    let radiusMeters:Double = 1000
    let identifier = "MyGeofence \(location)"

    let region = CLCircularRegion(center: location.coordinate, radius: radiusMeters, identifier: identifier)

    region.notifyOnEntry = true
    region.notifyOnExit = !region.notifyOnEntry

    return region
}

func getLocationsFromFireBase() -> [CLLocation] {
    var locations:[CLLocation] = []

    // .. populate with locations from DB

    return locations
}


//where you want to enable
let locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()

let locations = getLocationsFromFireBase()

for location in locations {
    let region = getRegionForLocation(location)
    startMonitoring(locationManager, region: region)
}

我正在掩饰如何启用位置访问(例如,您必须在 info.plist 中添加 NSLocationAlwaysUsageDescription),但显示了添加多个地理围栏的一般原则。您还需要向 CLLocationManager 添加委托,以便在设备进入或离开地理围栏时收到通知。