0
votes

I've worked with GeoFire before, and from what I remember, when you store a location using setLocation in GeoFire, the Firebase structure is somewhat like this:

{
  "3f7892b2-f304-4129-8416-0dc7216c2188" : {
    "g" : "9qc9p66045",
    "l" : {
        0 : 37,
        1 : -120
    }
  }
}

The problem is, I already have a realtime database up and running, and it has this structure:

{
  "3f7892b2-f304-4129-8416-0dc7216c2188" : {
    "username" : "bobby123",
    "name" : "john",
    "bio" : " I like dogs",
    "location" : {
        "lon" : 37,
        "lat" : -120
    }
  }
}

So it has other data in there, besides location (which I'm currently storing using CLLocation, and just updating child value).

So my question is, what would be the most practical way to run a GeoFire query? I realize I can just use setLocation, and have the id by the userid, and make a reference to that later when I'm loading the users into tableview, but I'm curious if it's possible to run a GFQuery on my data the way it's set up currently.

1

1 Answers

0
votes

From what I know you have to use the first structure you mentioned and then when you want the location for a user:

geoFireRef!.getLocationForKey(userID, withCallback: { (location, error) in
}

or whatever you call your ref. then you call do a query on that location (example a 100 km radius):

let locationQuery = geoFireRef!.query(at: location, withRadius: 100)
locationQuery?.observe(GFEventType.init(rawValue: 0)!, with: {(key, location) in
}

With that you get the keys and therefore the users with a usersRef.child(key).observeSingleEvent(of: .value, with: { snapshot in

etc.

From what I have read this is a fixed structure for GeoFire.