2016-06-17 2 views
1

mongoDB에 저장된 5m 포인트를 클러스터하려고합니다. 1 쿼리의 최대 결과는 현재 약 1m이므로 첫 번째 영상에는 PruneCluster을 사용하기로 결정했습니다. 점은 geoJSON 객체로 저장되며 두 점의 인덱스가 있습니다.Node.js mongodb에서 geojson을 프론트 엔드에서 사용할 수있는 객체에 전달하는 방법

내가 미리보기 전용 수집 및 경로 결과에서 LOC 필드가 반환, 다각형 내에서 포인트를 찾기 위해 기본 Node.js를 드라이버와 쿼리를 사용하여 MongoDB에 연결 :

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 
     ] 
    } 
    } 
] 

는 그럼 난 공용 폴더 /에서 스크립트 기능을 어떻게 든 그 결과를 전달해야합니다, 그래서 전단지지도에서 클러스터 표시 할 수 있습니다

비취로 내용을 인쇄 할 수는 있지만 원하는 모든 mongodb 데이터를 마커로 PruneCluster에 전달하는 방법을 모릅니다. 나는 전체 코드가 방향에 대한 요점만을 필요로하지 않는다. 누군가 나를 도울 수 있습니까?

답변

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); 
    ... 
}); 
+0

이 답변에 대한 감사합니다! 당신은 내가 어둠을 어떻게 잡을 지, db에서 얻는 geojson을 밖으로 나가는 지에 대한 방향을 가르쳐 주시겠습니까? – toHo

+0

와우 감사합니다! 원한다면 결과를 공유 할 코드를 작성하려고합니다. – toHo

+0

당신은 잘됐다! 소멸을 위해 spred 연산자 "..."의 사용에 관심이 있다면 여기를보십시오. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator –

관련 문제