gpt4 book ai didi

swift - 未从 firebase 数据库 swift 4 中获取数据

转载 作者:行者123 更新时间:2023-11-28 05:50:31 25 4
gpt4 key购买 nike

我只是想显示用户离该位置有多远,但我似乎无法将任何内容打印到我的控制台。如果我将代码放在 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)
}

}

Firebase Database

最佳答案

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)
}
}

关于swift - 未从 firebase 数据库 swift 4 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53147335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com