mongodb 地理位置搜寻

LBS,存储每个地点的经纬度坐标,搜寻附近的地点,建立地理位置索引可提高查询效率。

mongodb地理位置索引,2d2dsphere,对应平面和球面。


1.创建lbs集合存放地点坐标

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. use lbs;  
  2.   
  3. db.lbs.insert(  
  4.     {  
  5.         loc:{  
  6.             type: "Point",  
  7.             coordinates: [113.332264, 23.156206]  
  8.         },  
  9.         name: "广州东站"  
  10.     }  
  11. )  
  12.   
  13. db.lbs.insert(  
  14.     {  
  15.         loc:{  
  16.             type: "Point",  
  17.             coordinates: [113.330611, 23.147234]  
  18.         },  
  19.         name: "林和西"  
  20.     }  
  21. )  
  22.   
  23. db.lbs.insert(  
  24.     {  
  25.         loc:{  
  26.             type: "Point",  
  27.             coordinates: [113.328095, 23.165376]  
  28.         },  
  29.         name: "天平架"  
  30.     }  
  31. )  

2.创建地理位置索引

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. db.lbs.ensureIndex(  
  2.     {  
  3.         loc: "2dsphere"  
  4.     }  
  5. )  

3.查询附近的坐标

当前位置为:时代广场,

坐标:113.323568, 23.146436


搜寻附近一公里内的点,由近到远排序

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. db.lbs.find(  
  2.     {  
  3.         loc: {  
  4.             $near:{  
  5.                 $geometry:{  
  6.                     type: "Point",  
  7.                     coordinates: [113.323568, 23.146436]  
  8.                 },  
  9.                 $maxDistance: 1000  
  10.             }  
  11.         }  
  12.     }  
  13. )  

搜寻结果:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. { "_id" : ObjectId("556a651996f1ac2add8928fa"), "loc" : { "type" : "Point", "coordinates" : [ 113.330611, 23.147234 ] }, "name" : "林和西" }  

php代码如下:

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. // 连接mongodb  
  3. function conn($dbhost$dbname$dbuser$dbpasswd){  
  4.     $server = 'mongodb://'.$dbuser.':'.$dbpasswd.'@'.$dbhost.'/'.$dbname;  
  5.     try{  
  6.         $conn = new MongoClient($server);  
  7.         $db = $conn->selectDB($dbname);  
  8.     } catch (MongoException $e){  
  9.         throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);  
  10.     }  
  11.     return $db;  
  12. }  
  13.   
  14. // 插入坐标到mongodb  
  15. function add($dbconn$tablename$longitude$latitude$name){  
  16.     $index = array('loc'=>'2dsphere');  
  17.     $data = array(  
  18.             'loc' => array(  
  19.                     'type' => 'Point',  
  20.                     'coordinates' => array(doubleval($longitude), doubleval($latitude))  
  21.             ),  
  22.             'name' => $name  
  23.     );  
  24.     $coll = $dbconn->selectCollection($tablename);  
  25.     $coll->ensureIndex($index);  
  26.     $result = $coll->insert($dataarray('w' => true));  
  27.     return (isset($result['ok']) && !empty($result['ok'])) ? true : false;  
  28. }  
  29.   
  30. // 搜寻附近的坐标  
  31. function query($dbconn$tablename$longitude$latitude$maxdistance$limit=10){  
  32.     $param = array(  
  33.         'loc' => array(  
  34.             '$nearSphere' => array(  
  35.                 '$geometry' => array(  
  36.                     'type' => 'Point',  
  37.                     'coordinates' => array(doubleval($longitude), doubleval($latitude)),   
  38.                 ),  
  39.                 '$maxDistance' => $maxdistance*1000  
  40.             )  
  41.         )  
  42.     );  
  43.   
  44.     $coll = $dbconn->selectCollection($tablename);  
  45.     $cursor = $coll->find($param);  
  46.     $cursor = $cursor->limit($limit);  
  47.       
  48.     $result = array();  
  49.     foreach($cursor as $v){  
  50.         $result[] = $v;  
  51.     }    
  52.   
  53.     return $result;  
  54. }  
  55.   
  56. $db = conn('localhost','lbs','root','123456');  
  57.   
  58. // 随机插入100条坐标纪录  
  59. for($i=0; $i<100; $i++){  
  60.     $longitude = '113.3'.mt_rand(10000, 99999);  
  61.     $latitude = '23.15'.mt_rand(1000, 9999);  
  62.     $name = 'name'.mt_rand(10000,99999);  
  63.     add($db'lbs'$longitude$latitude$name);  
  64. }  
  65.   
  66. // 搜寻一公里内的点  
  67. $longitude = 113.323568;  
  68. $latitude = 23.146436;  
  69. $maxdistance = 1;  
  70. $result = query($db'lbs'$longitude$latitude$maxdistance);  
  71. print_r($result);  
  72. ?>  


演示php代码,首先需要在mongodb的lbs中创建用户和执行auth。方法如下:

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. use lbs;  
  2. db.createUser(  
  3.     {  
  4.         "user":"root",  
  5.         "pwd":"123456",  
  6.         "roles":[]  
  7.     }  
  8. )  
  9.   
  10. db.auth(  
  11.     {  
  12.         "user":"root",  
  13.         "pwd":"123456"  
  14.     }  
  15. )  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值