【发布时间】:2014-10-29 18:33:52
【问题描述】:
这里是球场。我有一个 Circles 集合,主要有两个属性:location 是一个点,radius 是一个以米为单位的距离。
用户还有一个profile.location Point 属性。
在我的出版物中,我想找到用户“加入”的所有圈子,因此根据每个圈子的radius 属性,找到他或她足够近的圈子。总而言之,它的外观如下:
Meteor.publish('circles', function() {
var curUser = Meteor.users.findOne({_id:this.userId});
if (curUser) {
return Circles.find({
location: {$near:
{$geometry:curUser.profile.location,$maxDistance:self.radius} // HERE
}
}));
}
this.ready();
});
self.radius 除外,这完全是代表我编造的术语。但是有可能实现这样的目标吗?
求解后编辑:
感谢Electric Jesus,我的匹配与多边形完美配合,因为圆目前还不是 GeoJSON 类型。 (因此它们不是可以查询的单个属性)所以我将我的圆圈转换为多边形!这是一个执行此操作的 JavaScript 函数:
function generateGeoJSONCircle(center, radius, numSides) {
var points = [];
var earthRadius = 6371;
var halfsides = numSides / 2;
//angular distance covered on earth's surface
var d = parseFloat(radius / 1000.) / earthRadius;
var lat = (center.coordinates[1] * Math.PI) / 180;
var lon = (center.coordinates[0] * Math.PI) / 180;
for(var i = 0; i < numSides; i++) {
var gpos = {};
var bearing = i * Math.PI / halfsides; //rad
gpos.latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(bearing));
gpos.longitude = ((lon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(gpos.latitude))) * 180) / Math.PI;
gpos.latitude = (gpos.latitude * 180) / Math.PI;
points.push([gpos.longitude, gpos.latitude]);
};
points.push(points[0]);
return {
type: 'Polygon',
coordinates: [ points ]
};
}
给你。我真的不知道我应该使用多少边,所以我也为此留下了一个论据。从那里,您可以使用 Electric Jesus 的答案来了解我要去的地方。不要忘记在你的多边形上放置一个 2dsphere 索引!
Circles._ensureIndex({'polygonConversion': "2dsphere"});
【问题讨论】:
-
没有。上一个已经回答了。这里的问题更复杂。
标签: mongodb collections meteor geometry