我只是想显示用户离这个位置有多远,但似乎无法将任何内容打印到我的控制台上。如果我将代码放在viewDidLoad中并手动放在“postLocations”的坐标中
我得到了距离,但我似乎无法得到任何打印与我目前的代码。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0] as CLLocation
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? Double else { return }
guard let longitude = dictionary["longitude"] as? Double else { return }
let currentLocation = Coordinate(long: userLocation.coordinate.longitude, lat: userLocation.coordinate.latitude)
let postLocations = Coordinate(long: longitude, lat: latitude)
if currentLocation.distance(to: postLocations) <= 100 {
print("You are \(currentLocation.distance(to: postLocations)) away from posts")
} else {
print("No posts in area")
}
}) { (error) in
print("There was an error getting posts", error)
}
}
最佳答案
在viewDidLoad
中尝试此代码,并检查控制台中的print语句是否已打印纬度和经度。
func fetchUserLocation() {
let ref = Database.database().reference().child("posts")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: AnyObject] else { return }
guard let latitude = dictionary["latitude"] as? String else { return }
guard let longitude = dictionary["longitude"] as? String else { return }
print("lat: \(latitude), lon: \(longitude)")
}) { (error) in
print("There was an error getting posts", error)
}
}
关于swift - 无法从Firebase数据库Swift 4中获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53147335/