我正在处理一个关于从Firebase获取数据并在我的代码中的其他地方使用该数据的简单问题。下面,我有一个函数,它检索一个位置的纬度和经度,并将它们存储在两个变量locationLat
和locationLong
中定义并初始设置为0.0。我在viewDidLoad
中调用此函数。
然后在didUpdateLocations
中访问保存纬度和经度的两个变量,以将用户的位置与从Firebase获取的值的纬度和经度进行比较。
我的问题:didUpdateLocations
中的if条件不会一直有效,当它没有时,纬度和经度打印0.0或nil并且它不运行if语句。
我知道observeSingleEvent本质上是异步的,但我不确定如何在我的代码中正确使用Firebase数据,尤其是在didUpdateLocations
委托函数中。任何建议都会很棒。
以下是与此问题相关的两个功能。第一个是我的函数,它从Firebase中检索纬度和经度。第二个函数是我的didUpdateLocations
函数,我的bug出现了。
func getLocationCoordinates() {
let ref = Database.database().reference().child("map").child(self.titleString)
ref.observeSingleEvent(of: .value) { (snapshot) in
let nameSnapshots = snapshot.childSnapshot(forPath: "title")
let val = nameSnapshots.value as! String
if self.locationName.text == val {
let latitude = snapshot.childSnapshot(forPath: "latitude")
let longitude = snapshot.childSnapshot(forPath: "longitude")
let latValue = latitude.value as! CLLocationDegrees
let lonValue = longitude.value as! CLLocationDegrees
self.locationLat = latValue
self.locationLong = lonValue
}
}
}
这是didUpdateLocations
功能
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.desiredAccuracy = kCLLocationAccuracyBest
let userLocation :CLLocation = locations[0] as CLLocation
let locationAddress: CLLocation = CLLocation(latitude: locationLat, longitude: locationLong)
print("loc latitude = \(locationAddress.coordinate.latitude)")
print("loc longitude = \(locationAddress.coordinate.longitude)")
let distanceFrom : CLLocationDistance = userLocation.distance(from: locationAddress)
if 300 >= distanceFrom {
checkInOutlet.isEnabled = true
print("we are here")
} else {
checkInOutlet.isEnabled = false
print("not here yet")
}
locationManager.stopUpdatingLocation()
}
由于您的getLocationCoordinates
和didUpdateLocations
是async
调用,您需要等到getLocationCoordinates
从firebase服务器给出结果并使用返回值到didUpdateLocations
方法。
为此,你可以使用completion
封闭。检查下面的示例代码:
func getLocationCoordinates(completion: @escaping (_ lat: CLLocationDegrees, _ lon: CLLocationDegrees)->()) {
let ref = Database.database().reference().child("map").child(self.titleString)
ref.observeSingleEvent(of: .value) { (snapshot) in
let nameSnapshots = snapshot.childSnapshot(forPath: "title")
let val = nameSnapshots.value as! String
if self.locationName.text == val {
let latitude = snapshot.childSnapshot(forPath: "latitude")
let longitude = snapshot.childSnapshot(forPath: "longitude")
let latValue = latitude.value as! CLLocationDegrees
let lonValue = longitude.value as! CLLocationDegrees
self.locationLat = latValue
self.locationLong = lonValue
completion(latValue, lonValue) //this will return your data
}
}
}
我用getLocationCoordinates
回调修改了你的completion
,当completion
从firebase服务器获取数据并从该调用返回lat
和lon
值时将调用它。
现在你需要在你的didUpdateLocations
方法中使用它,如下所示:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
manager.desiredAccuracy = kCLLocationAccuracyBest
let userLocation :CLLocation = locations[0] as CLLocation
//call your method like this
getLocationCoordinates { (lat, lon) -> () in
//use lat and lon which returned by above method
let locationAddress: CLLocation = CLLocation(latitude: lat, longitude: lon)
print("loc latitude = \(locationAddress.coordinate.latitude)")
print("loc longitude = \(locationAddress.coordinate.longitude)")
let distanceFrom : CLLocationDistance = userLocation.distance(from: locationAddress)
if 300 >= distanceFrom {
self.checkInOutlet.isEnabled = true
print("we are here")
} else {
self.checkInOutlet.isEnabled = false
print("not here yet")
}
self.locationManager.stopUpdatingLocation()
}
}