2017-03-15 19 views
2

我現在非常絕望,因爲我試圖在我的Firebase數據庫上使用GeoFire來查找附近的用戶。 我現在已經呆了兩天了。 我搜索谷歌和stackoverflow了很多,並嘗試了我在那裏發現的一切,但沒有任何成功。現在我最後的希望是自己創建這個線程,希望有人能幫助我。在Swift中使用GeoFire查詢並沒有給出有用的輸出

我的數據庫幾乎是這樣的:

users 
    user_id1 
     email: [email protected] 
     username: xxx 
    user_id2 
     email: [email protected] 
     username: yyy 
users_locations 
    user_id1 
     location 
      g: xxxx 
      l 
       0: 30.0000 
       1: 5.0000 
    user_id2 
     location 
      g: yyyy 
      l 
       0: 30.0010 
       1: 5.0010 

我不知道這是否是需要保存從用戶分隔的位置數據,但我也試圖將其與相同的結果保存這樣所以我認爲這並不重要:

users 
    user_id1 
     email: [email protected] 
     username: xxx 
     location 
      g: xxx 
      l 
       0: 30.0000 
       1: 5.0000 

現在的代碼我使用:

要寫入的位置爲我用下面的數據庫,這是工作的罰款:

let user = FIRAuth.auth()?.currentUser 
let uid = user?.uid 

let ref = FIRDatabase.database().reference() 
let geofireRef = ref.child("users_locations") 
let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!)) 

geoFire?.setLocation(myLocation, forKey: "location") 

現在我嘗試使用GeoFire查詢給我看在限定半徑的所有用戶正在打印沒有爲這一個:

let ref = FIRDatabase.database().reference() 
let geofireRef = ref.child("users_location") 
let geoFire = GeoFire(firebaseRef: geofireRef) 

let circleQuery = geoFire?.query(at: center, withRadius: 10) 
circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in 
     print("Key '\(key!)' entered the search are and is at location '\(location!)'") 
    }) 

我想通了,我得到的結果,如果我去給我實際的孩子。然後它會打印我的實際用戶位置和關鍵字,但這當然不是我想要的。

let geoFire = GeoFire(firebaseRef: geofireRef.child(uid!)) 

所以我想GeoFire通過我的UID在「users_locations」搜索並返回我所有的UID這是在我的定義半徑。

對我來說,它似乎只是在我的引用(users_location - > user_uid)中定義的子級中搜索名爲'location'的子級,並且如果我嘗試通過'users_location'查詢,我什麼都得不到。

我在做什麼錯? 我該如何使查詢搜索引用孩子,並給我回uid?

+0

你在哪裏定義'center':你應該這樣做在他.keyEntered處理? –

+0

我將中心設置在我的實際位置之前。我嘗試了一點,但在這個例子中,它是我的位置 – user3389960

+0

並且是''users_locations'中位於距'center中心10公里內的位置?因爲否則他們不會出現。 –

回答

1

請嘗試使用GeoFire來編寫位置。您不需要將它們存儲在該location密鑰下。

let ref = FIRDatabase.database().reference() 
let geofireRef = ref.child("users_locations") 
let geoFire = GeoFire(firebaseRef: geofireRef) 

geoFire?.setLocation(myLocation, forKey: uid!) 
+0

它似乎工作!非常感謝您提供快速解決方案。 – user3389960

3

使用Geofire,你有兩個數據列表:

  1. 對象的名單,在你的情況下,用戶
  2. 的這些對象的位置列表,維持並通過查詢Geofire

這兩個列表確實是分開的。從Geofire documentation(重點是我的):

假設您正在構建一個應用程序來評分酒吧,並存儲了酒吧的所有信息,例如,名稱,營業時間和價格範圍,在/bars/<bar-id>。稍後,您希望爲用戶添加搜索附近酒吧的可能性。這是GeoFire進來的地方。

您可以使用GeoFire,將條形碼ID作爲GeoFire鍵存儲每個條形圖的位置。然後GeoFire允許您輕鬆查詢哪些酒吧ID(鑰匙)在附近。 要顯示有關條的任何其他信息,可以加載查詢返回的每個條的信息,其地址爲/bars/<bar-id>

我強調了兩個最重要的位爲你的問題:

  1. 在用戶列表和它們的位置應該使用相同的密鑰(這是安德魯也指出了列表中的項目他的回答)。通過使用相同的密鑰,您可以輕鬆地查找用戶的位置,反之亦然。
  2. 您需要爲查詢中的每個鍵單獨加載用戶。

Andrew顯示瞭如何正確編寫每個用戶的位置。剩下的就是加載關於結果範圍內每個用戶的附加信息。

let usersRef = ref.child("users") 

let circleQuery = geoFire?.query(at: center, withRadius: 10) 
circleQuery?.observe(.keyEntered, with: { (key: String?, location: CLLocation?) in 

    print("Key '\(key!)' entered the search are and is at location '\(location!)'") 
    // Load the user for the key 
    let userRef = usersRef.child(key) 
    userRef.observeSingleEvent(FIRDataEventType.value, with: { (snapshot) in 
     let userDict = snapshot.value as? [String : AnyObject] ?? [:] 
     // ... 
    }) 
})