流星& MongoDB Geospatial - bounds - $ within

时间:2013-03-18 21:45:16

标签: javascript global-variables meteor

在我的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: '&copy; <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: '&copy; <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
      });
    });
});

2 个答案:

答案 0 :(得分:2)

修改更好的是,使用自定义的响应式数据源。我写了一个小教程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
  });
 }
});

在我的客户端代码中,我只保留客户端的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);
 }

答案 1 :(得分:0)

您在客户端上定义了map,因此该变量在服务器上不可用。由于Leaflet是客户端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
});