未从firebase数据库swift 4中提取数据

未从firebase数据库swift 4中提取数据,swift,firebase,firebase-realtime-database,swift4,Swift,Firebase,Firebase Realtime Database,Swift4,我只是想显示用户离这个位置有多远,但我似乎无法将任何内容打印到我的控制台上。如果我将代码放在viewDidLoad中并手动输入“postLocations”的坐标 我得到了距离,但我似乎无法用我目前的代码打印任何东西 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let userLocation: CLLocation = locations

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

    }
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
将userLocation:CLLocation=位置[0]设为CLLocation
让ref=Database.Database().reference().child(“posts”)
ref.observeSingleEvent(of:.值,其中:{(快照))位于
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}
让currentLocation=坐标(long:userLocation.Coordinate.longitude,lat:userLocation.Coordinate.latitude)
让postLocations=坐标(长:经度,纬度:纬度)

如果currentLocation.distance(to:postLocations)您不需要在
didUpdateLocations
中调用Firebase观察员。
您应该在
viewDidLoad
中调用Firebase观察员,以便您可以从Firebase数据库中获取用户位置。观察员观察用户位置后,您需要获取当前位置,以便您可以比较这两个位置。只需尝试此操作。

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

此方法是否被称为
didUpdateLocations
?是的,正在调用它检查
快照中的内容。值
可能返回了您的“guard”,比如类型不是
[String:AnyObject]
,可能尝试
[String:Any]
,看看会发生什么。您是否正在尝试获取您当前位置与firebase中用户位置之间的距离?如果您已经解决了问题,请忽略此问题。我正在尝试获取您当前位置与post位置@GregoryWilsonPullyattu之间的距离。我不确定我是否理解。您是否可以向我展示一个示例?可以吗你在viewDidLoad()中调用firebase observer出于某种原因,我仍然无法获得打印出来的纬度和经度。即使我将该代码放入我的
viewDidLoad()
中,我认为可能会返回纬度和经度