我對使用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()
})
謝謝!我正在嘗試,但仍然無法正常工作。我想這種方式(考慮到你的數據庫結構): 讓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
已解決,設置查詢中心時出現問題。謝謝。 – giacavicchioli
Firebase geofire-js:如何獲取靜態(固定)位置附近的按鍵列表:http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys -of-附近逐staticfixed-位置 –