如何在节点js的mongoose中查找坐标是否在多边形内

时间:2014-08-15 18:07:33

标签: javascript node.js mongodb mongoose geojson

我尝试了很多尝试,但都失败了。目前,我的查询没有失败,但它没有提供任何结果。我正在使用Mongoose v 3.8.9,它使用Mongodb v 1.4。 我认为我的坐标被正确编入索引,因为它适用于使用.geoNear()进行查询。但是,geoNear()只允许单个坐标,因此我无法指定g

以下是运行但未返回任何结果的示例。我甚至更改了其中一个文档,使其完全包含geoJSONpolygon中指定的四个点之一。

 var geoJSONpolygon = { type: 'Polygon',coordinates:[[43.6582231,-79.3988945],[43.6583683,-79.3980648],[43.6583143,-79.3979845],[43.6584212,-79.3975422]] }

  newLocation.find({}).where('pos').within().geometry(geoJSONpolygon).lean().exec(function(err,doc){
    console.log(doc);
    res.send(doc);
});

1 个答案:

答案 0 :(得分:1)

这就是我使用的,可能对你有帮助。

   Position.find(
        { geo :
                     { $geoWithin : { $box :
                                      [ [ box[0] , box[1] ] ,
                                        [ box[2] , box[3] ] ] } }

    }, function(err, response) {
        if (err) return err;

        console.log(response)

    });    

您也可以设置调试并检查结果:

mongoose.set('debug', true)
相关问题