5

I'm quite new to work with Firebase and its location library, GeoFire. Currently I'm facing some problem when structuring my data.

At the moment my db is something like this:

users
  facebook:xxxxx
    displayName: xx
    firstName: xx
    lastName: xx
    location:
      g: xxxx
      l:
        0: xxx
        1: xxx
  facebook:yyyyy
    displayName: yy
    firstName: yy
    lastName: yy
    location:
      g: yyyy
      l:
        0: yyy
        1: yyy

I would like to query on users that are near my current user logged in. To do so I can't understand what path I have to specify.

At the moment I'm doing like this (but that doesn't work):

saving current location

let root = Firebase(url: "myapp.firebaseio.com")
let usersRoot = root.childByAppendingPath("users")
geoFire = GeoFire(firebaseRef: usersRoot.childByAppendingPath(root.authData.uid))

geoFire.setLocation(currentLocation, forKey: "location") { (error) in
   if (error != nil) {
      print("An error occured: \(error)")
   } else {
      print("Saved location successfully!")
   }
}

retrieving other users near

let geoFire = GeoFire(firebaseRef: Firebase(url: "myapp.firebaseio.com").childByAppendingPath("users"))
let query = geoFire.queryAtLocation(currentLocation, withRadius: radius)

query.observeEventType(GFEventTypeKeyEntered, withBlock: {
   (key: String!, location: CLLocation!) in

   print("+ + + + Key '\(key)' entered the search area and is at location '\(location)'")
   self.userCount++
   self.refreshUI()
})

UPDATE

saving current location

let root = Firebase(url: "myapp.firebaseio.com")
geoFire = GeoFire(firebaseRef: root.childByAppendingPath("locations"))

geoFire.setLocation(currentLocation, forKey: root.authData.uid) { (error) in
   if (error != nil) {
      print("An error occured: \(error)")
   } else {
      print("Saved location successfully!")
   }
}

retrieving other users near

let geoFire = GeoFire(firebaseRef: Firebase(url: "myapp.firebaseio.com").childByAppendingPath("locations"))
let query = geoFire.queryAtLocation(currentLocation, withRadius: radius)

query.observeEventType(GFEventTypeKeyEntered, withBlock: {
   (key: String!, location: CLLocation!) in

   print("+ + + + Key '\(key)' entered the search area and is at location '\(location)'")
   self.userCount++
   self.refreshUI()
})
giacavicchioli
  • 334
  • 2
  • 14

3 Answers3

4

For GeoFire you must keep a segment in the tree that contains just the geolocation and then you have another segment in the tree that contains the other information about the items.

user_locations
  uid1:
    g: xxxx
    l:
      0: xxx
      1: xxx
  uid2:
    g: xxxx
    l:
      0: xxx
      1: xxx
user_profiles:
  uid1:
    name: "giacavicchioli"
  uid2:
    name: "Frank van Puffelen"

Also see: Query firebase data linked to GeoFire

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks! I'm trying right now, but still doesn't work. I tried this way (considering your db structure): let geoFire = GeoFire(firebaseRef: root.childByAppendingPath("user_locations")) let query = geoFire.queryAtLocation(currentLocation, withRadius: 10) query.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in print("+ + + + Key '\(key)' entered the search area and is at location '\(location)'") }) – giacavicchioli Feb 09 '16 at 21:08
  • Resolved, there was a problem when setting the query center. Thanks. – giacavicchioli Feb 09 '16 at 21:30
  • Firebase geofire-js : how to get list of keys of near by static(fixed) locations : http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys-of-near-by-staticfixed-locations – Ankit Maheshwari Apr 26 '17 at 11:55
2

To save all your records that match the query you have to store all of using something like:

    var allKeys = [String:CLLocation]()

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: {
        (key: String!, location: CLLocation!) in

        allKeys [key]=location
      }

and then use

    circleQuery.observeReadyWithBlock({
        print("All initial data has been loaded and events have been fired!")
    })
Jose Galvez
  • 552
  • 4
  • 6
0

Seems that there is an issue when setting up the GFEventType. That will be resolved it seems on the next version of GeoFire 1.3, for now you can do this...

let geoFire = GeoFire(firebaseRef: ref)

var allKeys = [String:CLLocation]()

let query = geoFire.queryAtLocation(coordinates, withRadius: 0.2)
print("about to query")
query.observeEventType(GFEventType.init(0), withBlock: {(key: String!, location: CLLocation!) in
    print("Key: \(key), location \(location.coordinate.latitude)")
    allKeys [key] = location
})

As you can see GFEventType.init(0)... that maps to the three types of enums that are not correctly mapped on GeoFire in Swift for some reason.

user2091936
  • 546
  • 2
  • 7
  • 28