【问题标题】:Data not being fetched from firebase database swift 4没有从firebase数据库swift 4中获取数据
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您无需在 didUpdateLocations 中调用 Firebase 观察者。 您应该在 viewDidLoad 中调用 Firebase 观察者,以便您可以从 Firebase 数据库中获取用户位置。在观察者观察到用户位置后,您需要获取当前位置,以便您可以比较这 2 个位置。试试这个。

    【讨论】:

    • 我不确定我是否理解。你能给我举个例子吗?
    • 你能在viewdidload中调用firebase观察者吗
    【解决方案3】:

    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()
    • 我认为可能会返回纬度和经度
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多