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

问题描述 投票:0回答:1

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

这是我的代码:

Firebase截图:

放置1 enter image description here

放置2 enter image description here

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

}

我觉得并且我非常自信我做错了,无论是我的查询还是我的地名。

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

Result from my console and my simulator

我认为where()方法是没有弄乱我的结果但是我不太舒服。

swift firebase google-cloud-firestore cllocation
1个回答
0
投票

如果您在某种情况下将位置与某个半径进行比较,则无法正常工作。你应该使用Firebase GeoFire代替。

GeoFire正在进入FireStore,一旦我与他们的家伙说话,但现在它不可能与FireStore。但你可以做的只是将你的位置数据放在Firebase中,在过滤了位置后,你可以再次在FireStore上查询更多数据。

你可以从GeoFireGeoFire DocGeoFire Git得到一些关于GeoFire Blog的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.