如何使用mongoid,rails和谷歌地图搜索“附近用户”
我有一个模型用户使用MongoDB存储的纬度和经度数据。如何使用mongoid,rails和谷歌地图搜索“附近用户”
我正在使用google maps javascript api v3来获取此数据。
什么是显示“附近用户”的最佳方式?
虽然DhruvPathak答案是正确的,这里是做地理相当于mongoid Ruby代码查询
User.where(:loc => {'$near' => [50,50]})
User.where(:loc => {'$near' => [50,50],'$maxDistance' => 5})
存储您的数据,以便它可以具有地理空间索引。 MongoDb对此有内置支持。然后,您可以轻松地使用内置的查询距离/邻近距离 。
看一看:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing
*Querying
The index can be used for exact matches:
db.places.find({ loc : [50,50] })
Of course, that is not very interesting. More important is a query to find points near another point, but not necessarily matching exactly:
db.places.find({ loc : { $near : [50,50] } })
The above query finds the closest points to (50,50) and returns them sorted by distance (there is no need for an additional sort parameter). Use limit() to specify a maximum number of points to return (a default limit of 100 applies if unspecified):
db.places.find({ loc : { $near : [50,50] } }).limit(20)
You can also use $near with a maximum distance
db.places.find({ loc : { $near : [50,50] , $maxDistance : 5 } }).limit(20)*
哇,mongoid岩石。 – kinet 2011-06-16 06:38:13
@ kinet纠正.... MONGODB岩石! :) – DhruvPathak 2011-06-17 06:05:55
很抱歉,此过于宽泛“,同时”太局部“。我们在这里帮助,而不是为你想。告诉我们您遇到的具体问题,或告诉我们什么孤立部分给您带来麻烦。 – coreyward 2011-06-16 04:29:43
对不起,我应该用不同的措词:/我明显可以想到各种解决方案,但我不知道mongo有geospacial索引。 DhruvPathak的回答很完美。 – kinet 2011-06-16 18:13:45