2014-02-25 45 views
1

我想显示离用户输入的流星地理位置最近的帖子。如何返回离地理位置最近的文档?

用户增加了对addpost模板后:

 //events.js (this code works nicely) 

Template.addpost.events ({ 
    'click .add': function (evt, tmpl) { 
     var title = tmpl.find('.title').value; 
     var lat = tmpl.find('.address-lat').value; 
     var long = tmpl.find('.address-long').value; 
     var loc = lat , long 
     Post.insert({title:title,loc:loc}); 

}) 

然后将它使用此代码显示在主页上:

//main.js (this works) 

Post = new Meteor.Collection("posts"); 

Template.main.posts = function() { 
    return Post.find(); 
} 

和HTML:

(this works) 

{{#each posts}} 
    <tr> 
     <td>{{title}}</td> 
    </tr> 
{{/each}} 


现在我的问题是:如何才能筛选这些职位,并显示用户指定的经度和纬度最近的100?

(这样做的mondodb代码可以在这里找到:http://docs.mongodb.org/manual/reference/operator/query/near/

回答

1

的文件表明,$near排序结果从最接近最远。因此,你可以做服务器上的以下(同时发布)或客户端(如果你把所有的已记录):从OP

Template.main.posts = function() { 
    var totalRecords = Post.find().count(); 
    return Post.find({ 
      loc : { 
       $near : { 
        $geometry : { 
         type : "Point", 
         coordinates: [ userLocLong, userLocLat ] 
        } 
       } 
      } 
     }, { 
      limit: 100 // Return only the nearest 100 results 
     } 
    ); 
}; 

注:

的代码实际上作品。刚发现你需要在这个格式中添加坐标到posts集合:

Post.insert({ 

     loc: { 
        type: "Point", 
        coordinates: [foo, bar], 
      } 
    }); 
+0

不适合我。该应用程序崩溃:/。 =>退出代码:8 =>您的应用程序崩溃。等待文件更改。 =>修改 - 重新启动。 – GeriTol

+0

@GeriTol恐怕没有足够的信息来开始调试。 –