-1

I have my data for posts structured like this in Firebase Realtime Database:

posts:
    key1:
        author: Jake
        location:
            g: xxxx
                0: yyyy
                1: yyyy
    key2:
        author: Daniel
        location:
            g: xxxx
                0: yyyy
                1: yyyy

How can I use GeoFire to query my database to find only the posts within a specified radius of me? Is there a way of changing the .indexOn rules in the database to perform this?

Thanks in advance

jacobsieradzki
  • 1,108
  • 2
  • 10
  • 32

1 Answers1

2

I think you need to separate your GeoFire from your UserDatabase

To Set Locations call

geoFire.setLocation(CLLocation(latitude: 37.7853889, longitude: -122.4056973), forKey: **UserID** )

Then you create the Circle Query

let center = CLLocation(latitude: 37.7832889, longitude: -122.4056973)
var circleQuery = geoFire.queryAtLocation(center, withRadius: 0.6) // 600m

the you make the call against the database

var queryHandle = query.observeEventType(.KeyEntered, withBlock: { (key: String!, location: CLLocation!) in
  print("Key '\(key)' entered the search area and is at location '\(location)'")
})

Every Key that is returned will be the UserID for your datebase and you can access the relevant path through a /User/{UserID}/...

Mark
  • 515
  • 2
  • 4
  • 8
  • Is there no way to do it by keeping the locations together with the post data? – jacobsieradzki Apr 18 '18 at 10:38
  • I'm not sure if you can do it that way, but even if you could, you then have permissions that need to be handled... because all the locations need to be exposed readable for all searches. – Mark Apr 18 '18 at 10:50