如何从 Firestore 访问和显示正确的字符串?

How to access and display the correct String from Firestore?

这是我在这个很棒的小组中的第二个 post,过去 5 天我一直在研究如何能够从 Firestore 获取数据并将其与我当前的用户进行比较 location。一切似乎都运行良好,除非我实时测试我的应用程序(使用我的 iPhone)。有时它会显示正确的位置,有时会崩溃或显示随机位置。我正在使用 where() 方法从我的 Firestore 访问数据,它似乎返回了我需要的数据。我觉得在我访问信息时我的文档中的名字不能正常显示。

这是我的代码:

Firebase 屏幕截图:

地点 1

地点 2

//Creating access to locationManager
var locationManager : CLLocationManager!

@IBOutlet weak var latLabel: UILabel!
@IBOutlet weak var lonLabel: UILabel!
@IBOutlet weak var place: UILabel!
@IBOutlet weak var placeImage: UIImageView!


//Storing the pass data that we got from the firt View

var placeName = String()
var latStore = String()
var lonStore = String()

var lonNumberStore = Double()
var latNumberStore = Double()
var fireLonMax = Double()
var fireLatMax = Double()
var fireLonMin = Double()
var fireLatMin = Double()


override func viewDidLoad() {


    //Here goes some code to display on the SecondViewController
    latLabel.text = latStore
    lonLabel.text = lonStore

    latMaxRead()
    latMinRead()
    lonMaxRead()
    lonMinRead()
}

//This is my button to test if I am in the correct place
@IBAction func updateLocation(_ sender: UIButton) {

    //
    if (fireLatMin...fireLatMax).contains(latNumberStore) && (fireLonMin...fireLonMax).contains(lonNumberStore){
        print("Is good place",fireLonMin,fireLonMax,fireLatMin,fireLatMax)
        place.text = placeName
    } else {
        print("Is not good place", fireLonMin,fireLonMax,fireLatMin,fireLatMax)
        place.text = "Bad"
    }

}

    func latMaxRead() {
    let docRef = Firestore.firestore()
        docRef.collection("places")
            .whereField("latMax", isGreaterThanOrEqualTo: latNumberStore)
            .getDocuments { (snapshot, error) in
                if error != nil {
                    print("Error getting documents: \(String(describing: error))")
                } else {
                    for document in (snapshot?.documents)! {
                        self.fireLatMax = document.data()["latMax"] as! Double
                        //This is where I pull the placeName on my Firebase
                        self.placeName = document.data()["placeName"] as! String 

                        print("Fire latMax:", self.fireLatMax)
                    }
                }
    }

}

func latMinRead() {
    let docRef = Firestore.firestore()
    docRef.collection("places")
        .whereField("latMin", isLessThanOrEqualTo: latNumberStore)
        .getDocuments { (snapshot, error) in
            if error != nil {
                print("Error getting documents: \(String(describing: error))")
            } else {
                for document in (snapshot?.documents)! {
                    self.fireLatMin = document.data()["latMin"] as! Double
                    self.placeName = document.data()["placeName"] as! String
                    print("Fire latMin: ", self.fireLatMin)
                }
            }
    }

}

func lonMaxRead() {
    let docRef = Firestore.firestore()
    docRef.collection("places")
        .whereField("lonMax", isGreaterThanOrEqualTo: lonNumberStore)
        .getDocuments { (snapshot, error) in
            if error != nil {
                print("Error getting documents: \(String(describing: error))")
            } else {
                for document in (snapshot?.documents)! {
                    self.fireLonMax = document.data()["lonMax"] as! Double
                    self.placeName = document.data()["placeName"] as! String
                    print("Fire lonMax: ", self.fireLonMax)
                }
            }
    }

}

func lonMinRead() {
    let docRef = Firestore.firestore()
    docRef.collection("places")
        .whereField("lonMin", isLessThanOrEqualTo: lonNumberStore)
        .getDocuments { (snapshot, error) in
            if error != nil {
                print("Error getting documents: \(String(describing: error))")
            } else {
                for document in (snapshot?.documents)! {
                    self.fireLonMin = document.data()["lonMin"] as! Double
                    self.placeName = document.data()["placeName"] as! String

                    print("Fire lonMin : ", self.fireLonMin)
                }
            }
    }

}

我觉得而且我非常有信心我做错了什么,无论是我的查询还是我的 placeName。

我的控制台和模拟器的结果:

Result from my console and my simulator

我认为 where() 方法不会影响我的结果,但我不太确定。

如果您将位置与某个半径内的位置进行比较,那么在这种情况下它将不起作用。您应该改用 Firebase GeoFire

GeoFire 正在处理 FireStore 我和他们的人谈过话但现在 FireStore 是不可能的。但是你可以做的只是将你的位置数据放在 Firebase 中,在过滤位置后你可以再次查询 FireStore 以获取更多数据。

您可以从 GeoFire Doc, GeoFire Git and GeoFire Blog 获得一些关于 GeoFire 的帮助。