2015-08-28 26 views

回答

2

這應該讓你開始

你需要在你的模式來定義一個屬性:

'location' : { 
    type: { type: String }, 
    coordinates: [] 
}, 

和索引的屬性作爲2dsphere

yourSchema.index({'location' : "2dsphere"}) 

比你能做到以下幾點:

//Model.geoNear(GeoJSON, options, [callback]) need a GeoJSON point to search in radius 
    var point = { type : "Point", coordinates : [data.coordinates.long, data.coordinates.lat] }; 
     YourModel.geoNear(point, { maxDistance : data.distance /coordinatesUtils.earthRadius, spherical : true }, function(err, results, stats) { 
      res.status(200); 
      res.json(results); 
     }); 

但也有幾點需要注意:

對於球形查詢運營商能夠正常運行,必須 距離轉換爲弧度,從弧度轉換爲應用程序所使用的距離單位 。

要轉換:到弧度的距離:用與測距距離 相同單位的球體(例如地球)的半徑除以半徑 。

弧度距離:乘以單位系統中您想要轉換的距離爲 的球體(例如地球)的弧度半徑值。

地球的半徑大約是3,959英里或6,371 公里。

here

帶到那裏去,在貓鼬是剝去GeoJSON的座標,並送他們喜歡舊的一雙蒙戈這使得近操作弧度,而不是米工作bug

現在可能會被修復,但我不確定。

您也可以閱讀文檔中geoNear in mongoose api site

你可以閱讀有關GeoJSON的here