【问题标题】:Node.js how to pass geojson from mongodb to object I can use in frontendNode.js如何将geojson从mongodb传递给我可以在前端使用的对象
【发布时间】:2016-06-17 20:40:48
【问题描述】:

我正在尝试对存储在 mongoDB 中的 5m 点进行聚类。 1 个查询的最大结果目前只有 1m 左右,所以我决定使用:PruneCluster 作为第一个视觉效果。点存储为 geoJSON 对象,并有一个 2dsphere 索引。

我使用本机 node.js 驱动程序连接到 mongodb 并查询以查找多边形内的点,这只会从集合和路由结果中返回 loc 字段以供预览:

router.get('/map', function(resq,res) {
  var MongoClient = mongodb.MongoClient;
  var url = 'mongodb://localhost:27017/airboost';

  MongoClient.connect(url, function(err, db) {
    if(err){
      console.log('Unable to connect to db ', err);
    } else {
        console.log('Connected!!!');

        var collection = db.collection('listingsF');

        collection.find({
           loc: {
             $geoWithin: {
                $geometry: {
                   type : "Polygon" ,
                       coordinates: [[[121.48521363894814,31.41360430073249],[...],[121.48865692654915,31.41168940543709]]]
                            }
                          }
                }
          },{loc:1}).toArray(function(err, result){

          if (err) {
            res.send(err);
          } else if (result.length) {
            res.send(result);
          } else {
            res.send('Nothing found');
          }
          db.close();
        });
      }
  });
});

我得到一个这样的对象列表:

[
  {
    "_id": "567f972bad55ac0797baa75b",
    "loc": {
      "type": "Point",
      "coordinates": [
        -85.84556526181,
        10.29620625503
      ]
    }
  },
  {
    "_id": "567f972bad55ac0797baa75c",
    "loc": {
      "type": "Point",
      "coordinates": [
        -54.943878777465,
        -34.964002797642
      ]
    }
  }
]

然后我需要以某种方式传递该结果以在 /public 文件夹中的脚本中运行,这样我就可以在传单地图上显示集群:

var pruneCluster = new PruneClusterForLeaflet();
  ... 
var marker = new PruneCluster.Marker(latitude, longitude);
pruneCluster.RegisterMarker(marker);
  ... 
leafletMap.addLayer(pruneCluster);

我可以以任何我想要的方式用玉打印内容,但我不知道如何将所有 mongodb 数据作为标记传递给 PruneCluster。我不需要完整的代码,只是指向一个方向。有人可以帮我吗?

【问题讨论】:

    标签: javascript node.js mongodb express pug


    【解决方案1】:

    您可以轻松地在您传递的对象的翡翠中创建一个变量:

    // server.js
    res.render('something', {
      something: mogoDBObject
    });
    
    // Your template.jade
    script.
      var myGeoJSONArray = !{something};
      // If you're getting problems parsing the array of objects
      // i.e. "something" becomes something like [[object Object], ...]
      // use myGeoJSONArray = JSON.parse('!{JSON.stringify(something)}');
    script(src='path/to/public/folder/main.js')
    
    // path/to/public/folder/main.js
    /* Do Something with your variable myGeoJSON */
    myGeoJSONArray.forEach(geoJSON=>{
      var marker = new PruneCluster.Marker(...geoJSON.loc.coordinates.reverse());
      marker.push(marker);
      ...
    });
    

    【讨论】:

    • 感谢您的回答!你能指出我如何从我从 db 获得的 geojson 中取出 lat、lng 的方向吗?
    • 哇,谢谢!如果您愿意,我将编写可以共享结果的代码。
    • 不客气!如果您对使用 spred 运算符“...”进行解构感兴趣,请查看此处:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    • 这是我得到的渲染:var myGeoJSONArray = [object Object],[object Object],...;
    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2018-06-11
    • 2017-10-09
    • 2016-03-14
    • 1970-01-01
    • 2020-04-26
    • 2021-08-20
    相关资源
    最近更新 更多