2017-02-12 70 views
1

語境Geofire如何計算Firebase查詢的邊界地理雜湊?

我用Geofire與火力地堡一起,而現在,我得到了真的很好奇Geofire如何執行其查詢。我從語義上理解,它是座標和半徑的函數,可以產生最小和最大哈希值。所以我覺得它的方式關頭可與火力地堡看起來像這樣

ref.child("users").orderByChild("g").startAt(minHash).endAt(maxHash).on('child_added', function(snapshot) { /* retrieved snapshot contains the geohashes in range */ }); 

當這兩個(最小值和最大值)geohashes計算離給定輸入。現在,這裏是問題

問題(S)

假設我上面所述是正確的,如何這兩個geohashes計算?當地理雜亂通常代表邊界矩形時,他們如何在某個圓形區域內返回結果?最後,不同大小的兩個geohashes如何具有相同的中心?

要澄清的是最後一部分:考慮下面的圖像

Typical geohashing steps

由於通過平分的區域分成更小的區域geohashing作品,不同的大小(最小和最大)的兩個散列怎麼可以有相同的中心點?

假設

我想,也許這是爲增加/減少哈希的原始值一樣簡單,但是這並沒有太大的意義,因爲增加/減少應該是相對的大小散列(可以說是「縮放」級別)和查詢半徑(如果我沒有弄錯的話)。

回答

3

GeoFire實際上對數據庫執行矩形區域的範圍查詢。該範圍是包含查詢中指定範圍的最小矩形。

然後,它在客戶端代碼檢查每個鍵查詢的中心的實際距離,並僅用於觸發內部查詢項key_entered/key_moved事件。

相關的代碼是here

// Determine if the location is within this query 
distanceFromCenter = GeoFire.distance(location, _center); 
isInQuery = (distanceFromCenter <= _radius); 

... 

// Fire the "key_entered" event if the provided key has entered this query 
if (isInQuery && !wasInQuery) { 
    _fireCallbacksForKey("key_entered", key, location, distanceFromCenter); 
} else if (isInQuery && oldLocation !== null && (location[0] !== oldLocation[0] || location[1] !== oldLocation[1])) { 
    _fireCallbacksForKey("key_moved", key, location, distanceFromCenter); 
} else if (!isInQuery && wasInQuery) { 
    _fireCallbacksForKey("key_exited", key, location, distanceFromCenter); 
}