结合MongoDB开发LBS应用-查看附近的人
最近负责的APP要上查看附件的人的功能
之前用coreseek做过类似附近的人的功能,觉得搭建索引整个下来太麻烦了
就开始寻找新的解决方案,查找到深入浅出Symfony2 – 结合MongoDB开发LBS应用,里面介绍了4个可选方案,推荐使用MongoDB来实现
首先加一点往MongoDB数据
了解下geoNear的参数使用,如下的查询语句可以查询为某经纬度为中心附近的用户(从近到远),也计算去距离了。
thinkphp实现的代码,距离单位都是km
<span style=“font-size: 12pt;“>$where[‘geoNear’] = ‘user_coordinate’;
$where[‘near’] = array($longitude,$latitude);
$where[‘spherical’] = true;
$where[‘maxDistance’] = $maxDistance/6371;
$where[‘distanceMultiplier’] = 6371;
$where[‘query’] = array(“status”=>1);
$res = $this->_user_coordinate_mod->command($where);
if (!empty($res[‘results’])) {
foreach ($res[‘results’] as $key => $value) {
$data[$key] = $value[‘obj’];
$data[$key][‘dis’] = $value[‘dis’];
}
}
return $data;</span>
```
就这样搞定了