我试图按照this tutorial为位置设置地理围栏,但我想使用从我的firebase数据库中获取的一系列信息创建地理围栏。有没有人知道我会怎么做,或有任何教程,他们可以为我链接?因为我对斯威夫特还很陌生,所以我很难想象我会怎么做。有人能帮我解释一下我会做什么,或者把我指给能解释这一点的人/某个地方吗?
最佳答案
像这样:
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添加一个委托,以便在设备进入或退出地理围栏时收到通知。
关于swift - 如何使用一系列信息创建地理围栏? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44266172/