2017-03-16 70 views
0

我有一个应用程序通过Geofire的纬度/经度坐标获取存储在我的Firebase中的用户周围的位置。Geofire +无限滚动 - 可以工作吗?

我现在希望能够在用户滚动到位置列表底部(又名无限滚动)时获取更多位置。

要做到这一点,我增加半径,反过来提取更多的位置 - 但我想避免提取已经加载相同的位置。 Geofire是否支持这样的事情(即,排除查询中已经加载的位置),还是必须通过一些手动处理来处理?

任何输入赞赏!

回答

1

Geofire将始终返回属于您的地理查询范围内的所有密钥。因此,如果您为越来越大的范围创建Geoqueries,则会得到重叠的键。

但是Geofire中的键非常小,通常比键所代表的实际物体小得多。所以如果你确保你没有重新加载你已经加载的对象,它可能仍然是可行的。

1

您想要diff针对您已获得的新结果集并仅处理新的键/值对。这可以使用array.filter方法来实现。所以,香草js真的。

+0

感谢罗恩,我确实想出了一种香草解决方案,它似乎足以有效地完成这项技巧。我会很快发布 –

0

使用纯香草JavaScript的解决方案,我是能够实现只追加新的位置与如下:

$scope.locations = ... // returned from service method 
var existingKeys = []; // keep track of existing firebase keys 

$scope.locations.forEach(function(location){ // Store the initial location ids 
    existingKeys.push(location.id); 
}); 

locationService.getNearby(5) // Set 3km as the new radius (was 2km) 
    .then(function(moreLocations){ 
    moreLocations.forEach(function(newLocation){ 

    /** Prevents pushing duplicates into array by comparing the existing keys **/ 
    if(existingKeys.indexOf(newLocation.id) === -1){ 
     $scope.locations.push(newLocation); 
     existingKeys.push(newLocation.id); // Append new keys to existingKeys[] 
    } 
}) 

注意 *这样做防止重装同一物体的X倍