Search code examples
swiftfirebaselocationgeofire

How can i get user nearby my location in geofire,Firebase


enter image description hereWith this code, I am getting only one value from Geolocs just key and location, not a nearby location.What is wrong with it

var geoFireRef: DatabaseReference?
    var geoFire: GeoFire?
    var myQuery: GFQuery?


func Geofire() {

        geoFireRef = Database.database().reference().child("Geolocs")
    geoFire = GeoFire(firebaseRef: geoFireRef!)
        self.geoFire?.setLocation(currentLocation!, forKey:Auth.auth().currentUser!.uid)

    myQuery = geoFire?.query(at: currentLocation!, withRadius: 100)

    myQuery?.observe(.keyEntered, with: { (key, location) in


        if key != Auth.auth().currentUser?.uid
        {
            let ref = Database.database().reference().child("Users").child(key)

            ref.observeSingleEvent(of: .value, with: { (snapshot) in
                let id = snapshot.key
            })

        }

    })

}

Solution

  • Keep your GeoFire Locations separate from everything with a key to reference the other additional data e.g. user info or post info. As mixing static and dynamic data would not be the most efficient way of storing the data.

    See my data structure and query here:

    Retrieving Keys From GeoFire within Radius in Swift