在我的 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.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)
这是加载时间的事情吗?在进行查询之前,我需要设置超时并等到地图加载完毕?
编辑这是我尝试过但不起作用的方法
服务器
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
});
});
});