【问题标题】:How would I create geofences using an array of information? [closed]如何使用一系列信息创建地理围栏? [关闭]
【发布时间】:2017-05-30 15:22:58
【问题描述】:

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

【问题讨论】:

  • 数据库中有什么?是纬度/经度吗?您通常需要一个点和一个半径来制作地理围栏。
  • 数据库包含纬度 + 经度以及区域的半径和标识符。感谢您提供下面的示例,我会试一试。

标签: swift firebase firebase-realtime-database cllocation


【解决方案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 添加一个委托,以便在设备进入或离开地理围栏时收到通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2016-09-01
    • 1970-01-01
    • 2014-11-08
    • 2013-03-30
    • 1970-01-01
    相关资源
    最近更新 更多