2013-08-19 149 views
1

我正在研究一個項目,我想在地圖上顯示標記。出版物訂閱觀察

這些標記應該從具有視口約束的服務器發佈。這意味着只發布標記在當前用戶視口內。

出版看起來是這樣的:

//server 
Meteor.publish('posts', function(bottom_left_x, bottom_left_y, upper_right_x, upper_right_y, limit) { 

    return Posts.find({locs: {$geoWithin: {$box: 
            [[bottom_left_x, bottom_left_y], 
            [upper_right_x, upper_right_y]]}}}, 
         {sort: {submitted: -1}, limit: limit}); 
}); 

我總是通過訂閱時調用這個函數我map_center變化:

//client 
google.maps.event.addListener(map, 'idle', function(event) { 
    var bounds = map.getBounds(); 

    var ne = bounds.getNorthEast(); 
    var sw = bounds.getSouthWest(); 
    postsHandle= Meteor.subscribe('posts', sw.lat(), sw.lng(), ne.lat(), ne.lng(), 10); 
}); 

到目前爲止一切工作正常。 此外,我在帖子上創建了一個觀察函數,它在調用「added」時呈現標記,並在調用「removed」時刪除。觀察是非常好的渲染新的標記和摧毀舊的

//client 
Posts.find().observeChanges({ 
    added: function(post) { 
    // when 'added' callback fires, add HTML element 
    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(post.locs.lat, post.locs.lng), 
     postId: post._id, 
     map: map, 
    }); 
},removed .... and so on 

這問題是觀察,觸發回調對整個帖子收集。但我只想顯示用戶視口內的標記。這就是爲什麼我通常必須這樣做:

//client 
Posts.find({locs: {$geoWithin: {$box: 
            [[bottom_left_x, bottom_left_y], 
            [upper_right_x, upper_right_y]]}}}, 
         {sort: {submitted: -1}, limit: limit}).observeChanges({ 

但那不可能。在minimongo中不支持GeoWithin,並且無法使用具有限制的集合調用oberserve。

有沒有人知道如何做到這一點? 也許有一種方法可以將我從subcription中直接獲取的帖子推送到地圖上,而無需在minimongo上使用查詢?

+0

如何發佈新的一套確實在服務器端這個過濾和訂閱它的客戶端?您可能不得不以某種方式發送界限。嗯..可能不行。 –

+0

我已經在服務器端有一個提示,並且每次我的視口更改時都會調用它。例如,在viewport_1中,我有3個標記,viewport_2提供2個製作者,而我的postsCollection在客戶端包含5個標記。這就是爲什麼我必須通過minimongo做一些查詢。但minimongo不提供必要的參數:( – chaosbohne

回答

1

解決方案非常簡單!

Meteor.autosubscribe(function() { 
    Meteor.subscribe('chat', { room: Session.get('currentRoom') }); 
}); 

如果要限制訂閱的視圖界面更改視圖的視圖比您必須使用自動訂閱。看來,autosubscribe照顧改變訂閱的論據:)的

Meteor.autosubscribe(function() { 
    var a = Session.get('bounds'); 
    if(a) 
    Meteor.subscribe('posts', a.swlat, a.swlng, a.nelat, a.nelng, 5); 
});