移动时,GeoFire会检索范围内的所有查询。

时间:2015-02-26 21:51:08

标签: firebase geofire

我试图在移动时从我的GeoFire数据库中检索所有元素。例如:当我四处走动时,我想要实时检索我周围元素的位置(移动或不移动)。

如果我使用以下代码:

var geoQuery = geoFire.query({
            center: [52.35500, 4.931000],
            radius: 0.1 //kilometers
});

var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
            console.log(key + " entered query at " + location + " (" + distance + " km from center)");
        });

我只在密钥更改位置(并输入我的查询)时收到更新。是否有可能将某个范围内的所有元素检索为一种快照?从那里监控实时?

我当然可以查询整个数据库,然后使用

GeoFire.distance(location1, location2)

但这看起来像是一个非常昂贵的选择。

2 个答案:

答案 0 :(得分:2)

您可以在移动时致电GeoQuery.updateCriteria(newQueryCriteria)更新查询中心。

请注意,您可能还想注册一个key_exited回调。


查看GeoFire API Reference

答案 1 :(得分:0)

您不需要查询整个数据库。您可以将地点存储在变量中,并在当前位置(查询中心)发生变化时计算新的距离。

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

let storyWords = story.split(' ');

let betterWords = storyWords.filter(function(words) {
     return words !== unnecessaryWords[0];
});

console.log(betterWords.join(' '));