首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于用户位置的GeoFire查询

关于用户位置的GeoFire查询
EN

Stack Overflow用户
提问于 2016-02-09 20:43:38
回答 3查看 5.8K关注 0票数 5

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

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

代码语言:javascript
复制
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

我想要查询的用户是接近我的当前用户登录。要做到这一点,我不明白我必须指定哪条路径。

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

保存当前位置

代码语言:javascript
复制
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!")
   }
}

检索附近的其他用户

代码语言:javascript
复制
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()
})

更新

保存当前位置

代码语言:javascript
复制
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!")
   }
}

检索附近的其他用户

代码语言:javascript
复制
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()
})
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-09 21:02:23

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

代码语言:javascript
复制
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"

另见:Query firebase data linked to GeoFire

票数 4
EN

Stack Overflow用户

发布于 2016-02-23 03:23:37

若要保存与查询匹配的所有记录,必须使用以下内容存储所有记录:

代码语言:javascript
复制
    var allKeys = [String:CLLocation]()

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

        allKeys [key]=location
      }

然后使用

代码语言:javascript
复制
    circleQuery.observeReadyWithBlock({
        print("All initial data has been loaded and events have been fired!")
    })
票数 2
EN

Stack Overflow用户

发布于 2016-03-06 03:10:41

在设置GFEventType时似乎存在问题。这将在下一个版本的GeoFire 1.3上得到解决,因为现在您可以这样做……

代码语言:javascript
复制
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
})

你可以看到GFEventType.init(0)..。它映射到由于某些原因在Swift中没有正确映射到GeoFire上的三种枚举类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35301799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档