Meteor & MongoDB Geospatial - bounds - $within
Meteor & MongoDB Geospatial - bounds - $within
在我的Meteor客户端中,我定义了一个映射对象:
Meteor.startup(function () {
map = L.map('map_canvas').locate({setView: true, maxZoom: 21});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
});
我使用它作为一个全局的方式,我可以访问它在Template.xxxx。事件,Template.yyy.rendered……(不知道这是不是最好的方法请输入)
所以在此之前一切都很好。
现在我需要执行一个地理空间查询,这只能在服务器端完成:
Meteor.startup(function () {
Meteor.publish("AllMessages", function() {
lists._ensureIndex( { location : "2d" } );
var bottomLeftLat = map.getBounds()._southWest.lat;
var bottomLeftLng = map.getBounds()._southWest.lng;
var topRightLat = map.getBounds()._northEast.lat;
var topRightLng = map.getBounds()._northEast.lng;
return lists.find( { "location": { "$within": { "$box": [ [bottomLeftLng, bottomLeftLat] , [topRightLng, topRightLat] ] } } } );
});
});
但是我的应用程序崩溃了,我得到:
Exception from sub ZeJzWHdF8xQg57QtF ReferenceError: map is not defined
at null._handler (app/server/Server.js:4:25)
at _.extend._runHandler (app/packages/livedata/livedata_server.js:815:31)
at _.extend._startSubscription (app/packages/livedata/livedata_server.js:714:9)
at _.extend.protocol_handlers.sub (app/packages/livedata/livedata_server.js:520:12)
at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:484:43)
是加载时间的问题吗?我需要设置超时,直到地图加载之前,我做我的查询?
edit这是我尝试过的,但不起作用
服务器Meteor.startup(function () {
Meteor.publish("AllMessages", function() {
lists._ensureIndex( { location : "2d" } );
return lists.find();
});
});
Meteor.methods({
getListsWithinBounds: function(bounds) {
return lists.find( { "location": { "$within": { "$box": [ [bottomLeftLng, bottomLeftLat] , [topRightLng, topRightLat] ] } } } );
}
});
Meteor.startup(function () {
map = L.map('map_canvas').locate({setView: true, maxZoom: 21});
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
bounds = {};
map.on('locationfound', function(e){
bounds.bottomLeftLat = map.getBounds()._southWest.lat;
bounds.bottomLeftLng = map.getBounds()._southWest.lng;
bounds.topRightLat = map.getBounds()._northEast.lat;
bounds.topRightLng = map.getBounds()._northEast.lng;
console.log(bounds);
Meteor.call("getListsWithinBounds", bounds, function(err, result) {
console.log('call'+result); // should log a LocalCursor pointing to the relevant lists
});
});
});
Edit: 使用自定义响应式数据源更好。我在这里写了一个小教程
旧帖:
我已经实现了类似的东西。在我的服务器代码中,我有以下发布函数:
// Publish those trails within the bounds of the map view.
Meteor.publish('trails', function(bounds){
if (bounds && bounds.southWest && bounds.northEast) {
return Trails.find({'coordinates': {'$within' :
{ '$box' : [bounds.southWest, bounds.northEast] }
}}, {
limit: 100
});
}
});
在我的客户端代码中,我只保留了一个客户端mapbounds集合。(基本上,它是一个只有一个文档的响应式模型)。
MapBounds = new Meteor.Collection(null);
我在客户端上有一个订阅,看起来像这样:
// Get trails that are located within our map bounds.
Meteor.autorun(function () {
Session.set('loading', true);
Meteor.subscribe('trails', MapBounds.findOne(), function(){
Session.set('loading', false);
});
});
最后,我的传单类在地图边界改变时更新边界模型。
onViewChange: function(e){
var bounds = this.map.getBounds()
, boundObject = {
southWest: [bounds._southWest.lat, bounds._southWest.lng],
northEast: [bounds._northEast.lat, bounds._northEast.lng]
};
if (MapBounds.find().count() < 1) MapBounds.insert(boundObject);
else MapBounds.update({}, boundObject);
}
您在客户机上定义了map
,因此该变量在服务器上不可用。你也不能在服务器上做任何映射处理,因为传单是一个客户端API。
如果你想让服务器从一个集合中返回一定范围内的列表,你应该在客户端计算范围,然后告诉服务器这些范围,这样它就可以为你找到信息。一个好的方法是使用Meteor.method
。
在服务器上,定义一个方法,该方法接受边界并返回这些边界内的列表:
Meteor.methods({
getListsWithinBounds: function(bounds) {
return lists.find({location: {"$within": {"$box": [bounds.bottomLeftLng, bounds.bottomLeftLat], [bounds.topRightLng, bounds.topRightLat]}});
}
});
然后在客户端调用服务器端的方法:
Meteor.call("getListsWithinBounds", bounds, function(err, result) {
console.log(result); // should log a LocalCursor pointing to the relevant lists
});
相关文章:
- 为什么无法根据谷歌地图地理编码geometry.bounds找到位置,但可以通过MongoDB中的geometry.vi
- Ui-view within ui-view
- Fancybox within Jquery Cycle Slide
- JQuery, Java Servlet Examplet within Eclipse WTP
- php ajax within ajax
- Javascript -add onlick within a function
- array within a switch javascript
- HREF within an IFrame with JQuery
- CFML within Javascript
- Google Maps Javascript API - bounds.contains method
- WebGL: glDrawElements exceeding vertex bounds
- Chrome.extension.sendMessage within chrome.tabs.create
- BXSlider within wordpress
- 谷歌地图 - 使用map.fitBounds (bounds);.
- Javascript window.location within php returned by json
- JavaScript Scope within factories in AngularJS
- Input text to a link within an <iframe>
- items not within the <ul></ul>
- Javascript Regex adding an attribute within <img> and
- Meteor & MongoDB Geospatial - bounds - $within