用户位置的 GeoFire 查询
Posted
技术标签:
【中文标题】用户位置的 GeoFire 查询【英文标题】:GeoFire query on User location 【发布时间】:2016-02-09 20:43:38 【问题描述】:我对使用 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()
)
【问题讨论】:
【参考方案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"
另见:Query firebase data linked to GeoFire
【讨论】:
谢谢!我现在正在尝试,但仍然无法正常工作。我试过这种方式(考虑到你的数据库结构): 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)'进入搜索区域,在位置'(location)'") ) 已解决,设置查询中心时出现问题。谢谢。 Firebase geofire-js:如何获取静态(固定)位置附近的键列表:***.com/questions/43632746/…【参考方案2】:要保存与查询匹配的所有记录,您必须使用以下方式存储所有记录:
var allKeys = [String:CLLocation]()
circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock:
(key: String!, location: CLLocation!) in
allKeys [key]=location
然后使用
circleQuery.observeReadyWithBlock(
print("All initial data has been loaded and events have been fired!")
)
【讨论】:
【参考方案3】:似乎在设置 GFEventType 时出现了问题。这将在下一个版本的 GeoFire 1.3 上得到解决,现在你可以这样做......
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 的三种类型的枚举。
【讨论】:
以上是关于用户位置的 GeoFire 查询的主要内容,如果未能解决你的问题,请参考以下文章
如果没有创建Geofire,我如何检查Geofire是否将用户注销?
如果没有创建 Geofire,我如何检查 Geofire 以注销用户?
将 GeoFire 查询加载到 RecyclerView 时应用程序崩溃