在我的 Meteor 客户端中,我定义了一个 map 对象:
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.events、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)
这是加载时间的问题吗?我需要设置超时并等到 map 加载后再执行查询?
编辑这是我尝试过的方法,但不起作用
服务器
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
});
});
});
最佳答案
编辑:更好的是,使用自定义响应式(Reactive)数据源。我写了一个小教程here .
旧帖子:
我已经实现了类似的东西。在我的服务器代码中,我有以下发布功能:
// 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
});
}
});
在我的客户端代码中,我保留了仅客户端的 map 边界集合。 (基本上,它是一种只有一个文档的响应式(Reactive)模型)。
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);
});
});
最后,我的传单类会在 map 边界发生变化时更新边界模型。
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);
}
关于javascript - Meteor 和 MongoDB 地理空间 - 边界 - $within,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487551/