2016-02-09 27 views
5

我对使用Firebase及其位置库GeoFire非常新颖。目前我在构建数据时遇到了一些问题。用户位置上的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() 
}) 

个UPDATE

保存当前位置

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

回答

4

对于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

+0

谢谢!我正在尝试,但仍然无法正常工作。我想这种方式(考虑到你的数据库结构): 让geoFire = GeoFire(firebaseRef:root.childByAppendingPath( “user_locations”)) 令查询= geoFire.queryAtLocation(currentLocation,withRadius:10) query.observeEventType(GFEventTypeKeyEntered, withBlock:{ (key:String !, location:CLLocation!) print(“+ + + + Key'\(key)'进入搜索区域,位于'\(location)'”) }) – giacavicchioli

+0

已解决,设置查询中心时出现问题。谢谢。 – giacavicchioli

+0

Firebase geofire-js:如何获取静态(固定)位置附近的按键列表:http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys -of-附近逐staticfixed-位置 –

1

要保存与查询匹配的所有记录,你必须储存所有使用类似的:

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

设置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上的枚举。