Search code examples
swiftfirebasegoogle-cloud-firestorecllocation

How to access and display the correct String from Firestore?


This is my second post in this awesome group, I have work in the past 5 days on how to be able to get data from Firestore and compare this to my current user location. Everything seems to be working well, except when I test my app in real time (with my iPhone). Sometimes it shows the correct place and other times it crashes or shows a random place. I'm working with the where() method to access the data from my Firestore and it seems that it is returning what I need. I feel that my name in my document is not working correctly at the point where I access the information.

Here is my code:

Firebase screenshots:

Place 1 enter image description here

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

}

I feel and I'm super confident that I'm doing something wrong, either with my Queries, or my placeName.

Results from my console and my simulator:

Result from my console and my simulator

I think the where() method is the one that is not messing around with my result but I'm not quite shure.


Solution

  • If you are comparing location with in some radius in that case it would not work. You should use Firebase GeoFire instead.

    GeoFire is under process to come in FireStore once I had words with their guy but right now it is not possible with FireStore. But what you can do is just put your location data inside Firebase and after filtering the location you can again query on FireStore for further data.

    You can get some help regarding GeoFire from GeoFire Doc, GeoFire Git and GeoFire Blog.