【发布时间】:2019-04-08 09:30:16
【问题描述】:
我只是想显示用户离该位置有多远,但我似乎无法将任何东西打印到我的控制台上。如果我将代码放在 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? -
是的,它被称为
-
检查
snapshot.value中的内容,也许你的 'guard` 返回了,比如类型不是[String: AnyObject],也许试试[String: Any],看看会发生什么。 -
您是否要获取当前位置与 Firebase 中用户位置之间的距离?如果你已经解决了你的问题,请忽略这个。
-
我正在尝试获取您当前位置与发布位置@GregoryWilsonPullyattu 之间的距离
标签: swift firebase firebase-realtime-database swift4