2016-02-09 25 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上的枚舉。