我尝试使用名为 getLocationForKey (Geofire) 的函数获取用户的位置。位置存储在 Firebase 中。我在 func tableview 中调用这个函数:
// Get user Distance
var myLocation = CLLocation()
myLocation = getUserLocation(ref.authData.uid)
print("myLocation: [\(myLocation.coordinate.latitude), \(myLocation.coordinate.longitude)]")
getUserLocation 如下所示:
func getUserLocation(userID: String) -> CLLocation {
var userLocation = CLLocation()
print(userID)
let geoFire = GeoFire(firebaseRef: ref.childByAppendingPath("locations"))
geoFire.getLocationForKey(userID, withCallback: { (location, error) in
if (location != nil)
{
userLocation = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
print("userLocation: [\(userLocation.coordinate.latitude), \(userLocation.coordinate.longitude)]")
}
else if (error != nil)
{
print("An error occurred getting the location for \(userID): \(error.localizedDescription)")
}
else
{
print("GeoFire does not contain a location for \(userID)")
}
})
return userLocation
}
结果,我得到了这个:
我的位置:[0.0, 0.0]
用户位置:[123.456, 123.456]
这对我来说似乎很奇怪,因为不知何故 getLocationForKey 中的代码被跳过了,我首先得到 myLocation,然后是 userLocation。有谁知道为什么?任何帮助表示赞赏,谢谢!