Search code examples
iosswiftfirebasefirebase-realtime-databasegeofire

GeoFire Query on lists of data


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


Solution

  • 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}/...