Swift 如何使用一系列信息创建地理围栏?
我正试图继续为位置设置地理围栏,但我想使用从Firebase数据库中获取的一系列信息创建地理围栏。有没有人知道我会怎么做,或者有什么教程可以帮我链接?我对斯威夫特还很陌生,所以我很难想象自己会怎么做。有人能帮我解释一下我会做什么,或者给我指一个可以解释这一点的人/地方吗 类似这样:Swift 如何使用一系列信息创建地理围栏?,swift,firebase,firebase-realtime-database,cllocation,Swift,Firebase,Firebase Realtime Database,Cllocation,我正试图继续为位置设置地理围栏,但我想使用从Firebase数据库中获取的一系列信息创建地理围栏。有没有人知道我会怎么做,或者有什么教程可以帮我链接?我对斯威夫特还很陌生,所以我很难想象自己会怎么做。有人能帮我解释一下我会做什么,或者给我指一个可以解释这一点的人/地方吗 类似这样: func startMonitoring(_ manager:CLLocationManager, region:CLCircularRegion) { if !CLLocationManager.isMon
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添加一个委托,以便在设备进入或离开地理围栏时收到通知。数据库中有什么?是经纬度吗?制作地理围栏通常需要一个点和一个半径。数据库有纬度+经度以及半径和区域标识符。谢谢下面的例子,我来试一试。