用户位置上的GeoFire查询
与Firebase及其位置库GeoFire一起工作是相当新的。 目前我在构build数据时遇到了一些问题。
目前我的数据库是这样的:
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
我想查询我的当前用户login附近的用户。要做到这一点,我不明白我必须指定什么path。
此刻我正在做这样的事情(但那不行):
节省当前位置
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,您必须在包含地理位置的树中保留一段,然后在树中包含有关项目的其他信息的另一个段。
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"
另请参阅: 查询与GeoFire链接的Firebase数据
要保存与查询相匹配的所有logging,必须使用以下内容来存储所有logging:
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!") })
似乎设置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)…映射到三种types的枚举,因为某些原因,在Swift中没有正确映射到GeoFire上。