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

时间:2017-05-30 15:22:58

标签: swift firebase firebase-realtime-database cllocation

我正在尝试关注this tutorial设置地理位置的地理位置,但我想使用从Firebase数据库中获取的一系列信息来创建地理围栏。有谁知道我会怎么做或有任何他们可以链接给我的教程?因为我对Swift很陌生,所以我很想知道如何做到这一点。有人可以帮忙解释一下我会做什么或者指向某个/某个可以解释这个问题的人吗?

1 个答案:

答案 0 :(得分:1)

这样的事情:

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添加委托,以便在设备进入或退出地理围栏时收到通知。