未从 firebase 数据库中获取数据 swift 4
Data not being fetched from firebase database swift 4
我只是想显示用户离该位置有多远,但我似乎无法将任何内容打印到我的控制台。如果我将代码放在 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)
}
}
您不需要在 didUpdateLocations
中调用 Firebase 观察器。
您应该在 viewDidLoad
中调用 Firebase 观察器,以便您可以从 Firebase 数据库中获取用户位置。观察者观察到用户位置后,需要获取当前位置,以便比较2个位置。试试这个。
在viewDidLoad
中尝试这段代码,检查控制台中的打印语句是否打印了经纬度。
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)
}
}
在 viewDidLoad()
上尝试通过调用此方法访问 Firebase 数据库:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}
我只是想显示用户离该位置有多远,但我似乎无法将任何内容打印到我的控制台。如果我将代码放在 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)
}
}
您不需要在 didUpdateLocations
中调用 Firebase 观察器。
您应该在 viewDidLoad
中调用 Firebase 观察器,以便您可以从 Firebase 数据库中获取用户位置。观察者观察到用户位置后,需要获取当前位置,以便比较2个位置。试试这个。
在viewDidLoad
中尝试这段代码,检查控制台中的打印语句是否打印了经纬度。
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)
}
}
在 viewDidLoad()
上尝试通过调用此方法访问 Firebase 数据库:
func getFirebaseUserLocation() {
let ref = Database.reference(withPath: "posts")
ref.observe(.value, with: { (snapshot) in
for child in snapshot.children {
if let postSnapshots = child as? DataSnapshot {
for postSnapshot in postSnapshots.children {
guard let postDictionary = postSnapshot.value as? [String: Any] else { return }
guard let latitude = postDictionary["latitude"] as? String, let longitude = postDictionary["longitude"] as? String else { return }
print(latitude, longitude)
// here you can access your latitude and longitude as Strings from Firebase Database
}
}
}
}
}