gpt4 book ai didi

javascript - Meteor & MongoDB 地理空间 - 边界 - $within

转载 作者:行者123 更新时间:2023-11-29 22:15:17 24 4
gpt4 key购买 nike

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

这是加载时间的问题吗?我需要设置超时并等到 map 加载后再执行查询吗?

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: '&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
});
});
});

最佳答案

编辑:更好的是,使用自定义 react 数据源。我写了一个小教程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 集合。 (基本上,这是一个只有一个文档的 react 模型)。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com