将Geofire与现有的Firebase数据库集成 [英] Integrating Geofire with existing firebase database
问题描述
我已经查看了 Geofire 的github Fish示例,但是仍不清楚如何在我的应用程序中实现它.我的用例:我的应用程序向登录用户显示事件/事件.当前在我的应用程序中,我在firebase ref(events)上有firebase侦听器.
I have gone through the github Fish examples for Geofire but still unclear of how to implement it within my app. My use case: My app shows events/meetups to logged in users. Currently within my app I have firebase listener at a firebase ref(events).
database.ref('events').orderByChild('startTime').on('value', (snapshot) => {
const feed = snapshot.val() || {}
})
当前,在初始加载时,客户端应用会在事件ref处获取整个对象,然后将每个事件数据显示在表中(每行一个事件)给用户.
Currently, at the initial load, the client app fetches the entire object at the events ref and then each of the events data is displayed to the user in a table(each row an event).
我了解,通过geofire,我可以使用set()函数来保存每个事件键及其地理位置[lat,lng].然后使用查询,我可以检索属于用户当前位置(html5地理位置)指定半径范围内的事件键列表.但是,由于键没有附加数据,我该怎么办?我是否要在事件"引用中为每个事件键设置单独的打开"侦听器?然后显示事件数据?
I understand that with geofire I can use the set() function to save each event key along with its geolocation[lat, lng]. And then using query I can retrieve the list of the event keys that fall within the specified radius to users current location(html5 geolocation). But What do I do with the keys, since there is no data attached to them, do I set individual "on" listeners for each event key at the Events ref? and then display the events data?
推荐答案
使用Geofire时,最终会在JSON树中出现两个分支:一个分支包含对象的地理数据,另一个分支包含实际数据.
When you use Geofire, you end up with two branches in your JSON tree: one with the geodata for objects and one with the actual data.
objects
objectId1: { ... }
objectId2: { ... }
objectId3: { ... }
geodata
objectId1: { ... }
objectId2: { ... }
objectId3: { ... }
当您针对 geodata
节点触发GeoQuery时,您将获得属于该查询范围的对象的键.因此,例如 objectId2
和 objectId3
.
When you fire a GeoQuery against the geodata
node, you get back the keys of the objects that fall within the range of that query. So for example objectId2
and objectId3
.
然后,您可以使用该键在 objects
下查找相应的JSON对象.例如,当使用 key_entered 代码>
:
You then use that key to look up the corresponding JSON object under objects
. For example when using key_entered
:
geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
ref.child("objects").child(key).once("value", function(snapshot) {
console.log(snapshot.val());
});
});
这篇关于将Geofire与现有的Firebase数据库集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!