meteor - Publications Subscriptions Observe -
i working on project want show markers on map.
these markers should published server viewport-constraint. means markers published inside current users viewport.
the publication looks this:
//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}); });
i call function via subscription when map_center changes:
//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); });
till works fine. further created observefunction on posts, renders marker when "added" called , remove when "removed" called. observe render new markers , destroy old ones
//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 .... , on
that problem observe-callback triggered on whole posts-collection. want show markers inside users viewport. thats why have this:
//client posts.find({locs: {$geowithin: {$box: [[bottom_left_x, bottom_left_y], [upper_right_x, upper_right_y]]}}}, {sort: {submitted: -1}, limit: limit}).observechanges({
but thats not possible. geowithin not supported inside minimongo , not possible call oberserve collection has limit.
has idea how accomplish this? maybe there way push posts subcription directly map without using query on minimongo?
the solution easy !
meteor.autosubscribe( function () { meteor.subscribe( 'chat', { room: session.get( 'currentroom' ) } ); } );
if want limit subscription maps viewport changing viewport-bounds, have use autosubscribe. seems autosubscribe takes care of changing subscription-arguments :)
meteor.autosubscribe( function () { var = session.get('bounds'); if(a) meteor.subscribe( 'posts', a.swlat, a.swlng, a.nelat, a.nelng, 5 ); } );
Comments
Post a Comment