一尘不染

用户位置上的GeoFire查询

swift

我对使用Firebase及其位置库GeoFire感到很陌生。目前,我在构造数据时遇到一些问题。

目前,我的数据库是这样的:

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

我想查询当前登录用户附近的用户。为此,我无法理解必须指定的路径。

目前,我正在这样做(但这不起作用):

保存当前位置

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

检索附近的其他用户

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()
})

更新

保存当前位置

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

检索附近的其他用户

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()
})

阅读 240

收藏
2020-07-07

共1个答案

一尘不染

对于GeoFire,您必须在仅包含地理位置的树中保留一个分段,然后在树中保留另一个包含有关项目的其他信息的分段。

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"
2020-07-07