如何找到如果一个坐标是在一个多边形在猫鼬节点js

How to find if a coordinate is within a polygon in mongoose for node js

本文关键字:一个 节点 js 多边形 如果 坐标 何找      更新时间:2023-09-26

我尝试了很多不同的尝试,但都失败了。目前,我的查询没有失败,但它没有为我提供任何结果。我正在使用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);
});

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

   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)