数据库/地理位置。寻找附近的人 [英] Database / geolocation. Finding people nearby

查看:406
本文介绍了数据库/地理位置。寻找附近的人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我使用地理位置功能时,我可以保存这些地图用户在服务器上的最后已知位置大约一个小时,然后我从数据库集合中删除位置(我使用MongoDb)

每次,我将这个位置与所有最近签到的人(位置集合中的那些人)进行比较,以查找接近的人。

这种方法看起来不像虽然效率很高,但人数可能会非常庞大​​,这意味着要重复一个长长的名单,并将职位与在不同国家的人员进行比较。



我在考虑划分分成每个国家或时区的多个表格。这会阻止应用程序搜索得太远。



任何建议或想法?



谢谢

解决方案

我会在这里使用MongoDB 2.4的地理空间支持。对于每个用户,以下列格式(它是GeoJSON)存储位置:

  {
_id:... ,
名称:...,
last_known_location:{
type:Point,
coordinates:[longitude,latitude]
}

$ / code>

并设置以下索引:

  db.location.ensureIndex({last_known_location:2dsphere}); 

现在您可以使用以下查询轻松搜索与当前用户相关的所有人员:

  db.location.find({
last_known_location:{
$ near:{
$ geometry:{
type:Point,
coordinates:[-0.1485302,51.5250157]
}
},
$ maxDistance:10
}
});

使用$ maxDistance,您可以控制距离所提供坐标多少米的最近用户。


I am currently trying to create a small web app that will find other people nearby who are using it as well.

When I use the geolocation feature, I save the "Last known location" of the user on the server for about an hour, after which I remove the location from the database collection (I use MongoDb)

Every time, I compare the position with all the people who have recently checked-in (those in the location collection), to find people who are close.

This method doesn't seem efficient though, the number of people could be huge, which would mean iterating a long list and comparing positions with people who are in different countries.

I'm thinking about dividing into multiple tables for each country or timezone. Which would prevent the app from searching too far.

Any advice or thoughts ?

Thanks

解决方案

I would use MongoDB 2.4's geospatial support here. For each user, store the location in the following format (it's GeoJSON):

{
    _id: ...,
    name: ...,
    last_known_location: { 
        "type": "Point",
        "coordinates": [ longitude, latitude ]
    }
}

And set the following index:

db.location.ensureIndex( { last_known_location: "2dsphere" } );

Now you can very easily search for all the people close to the current user, with the following query:

db.location.find( {
    last_known_location: {
        $near: { 
            $geometry: { 
                "type" : "Point", 
                "coordinates" : [ -0.1485302, 51.5250157 ] 
            }
        }, 
        $maxDistance: 10 
    } 
} );

With $maxDistance you can control how many meters away from the provided coordinates the "closest" users might be.

这篇关于数据库/地理位置。寻找附近的人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆