GeoFire request for user location

I am new to working with Firebase and its GeoFire location library. I am currently having some problems structuring my data.

At the moment, my db looks 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 request users who are next to my current user logging in. For this, I cannot figure out which path I should point out.

I'm currently doing this (but this does not work):

save 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!") } } 

fetching other users next to

 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

save 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!") } } 

fetching other users next to

 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() }) 
+5
source share
3 answers

For GeoFire, you must save a segment in a tree that contains only geolocation, and then you have another segment in the tree that contains different information about the elements.

 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 Database Data Associated with GeoFire

+4
source

To save all your records matching the query, you must save everything 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!") }) 
+1
source

There seems to be a problem setting up GFEventType. This will be enabled in the next version of GeoFire 1.3, now you can do it ...

 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) ..., which displays three types of enumerations that for some reason do not display correctly on GeoFire in Swift.

0
source

Source: https://habr.com/ru/post/1242648/


All Articles