I'm pretty desperate right now because I'm trying to use GeoFire on my Firebase Database to find nearby users. I'm pretty much stuck for two days now. I searched Google and stackoverflow a lot and tried everything I found there but without any success. Now my last hope is to create this thread myself and hope someone can help me.
My database pretty much looks like this:
users
user_id1
email: [email protected]
username: xxx
user_id2
email: [email protected]
username: yyy
users_locations
user_id1
location
g: xxxx
l
0: 30.0000
1: 5.0000
user_id2
location
g: yyyy
l
0: 30.0010
1: 5.0010
I don't know if it is necessary to save the location data seperated from the users but I also tried to save it like this with the same results so I think it doesn't matter:
users
user_id1
email: [email protected]
username: xxx
location
g: xxx
l
0: 30.0000
1: 5.0000
Now the code I'm using:
To write the location into the database I use the following, which is working fine:
let user = FIRAuth.auth()?.currentUser
let uid = user?.uid
let ref = FIRDatabase.database().reference()
let geofireRef = ref.child("users_locations")
let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!))
geoFire?.setLocation(myLocation, forKey: "location")
Now I try to use a GeoFire Query to show me all users in a defined radius which is printing nothing for this one:
let ref = FIRDatabase.database().reference()
let geofireRef = ref.child("users_location")
let geoFire = GeoFire(firebaseRef: geofireRef)
let circleQuery = geoFire?.query(at: center, withRadius: 10)
circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in
print("Key '\(key!)' entered the search are and is at location '\(location!)'")
})
I figured out that I get a result if I go to my actual uid child. Then it print my actual users location and key but that's of course not what I want to have.
let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!))
So I want GeoFire to search through my uid's in 'users_locations' and return me all uid's which are in my defined radius.
To me, it seems like it is only searching for the child named 'location' in the child defined in my Reference (users_location -> user_uid) and if I try to query over 'users_location', I get nothing.
What am I doing wrong? How can I make the query search reference child and give me the uid back?
center
? – Generalistusers_locations
within 10 kilometers of thatcenter
? Because otherwise they won't show up. – Generalist