• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP地理位置搜寻并计算距离

php 搞代码 4年前 (2022-01-22) 15次浏览 已收录 0个评论

本篇文章主要介绍PHP地理位置搜寻并计算距离,感兴趣的朋友参考下,希望对大家有所帮助。

地理位置搜寻
LBS,存储每个地点的经纬度坐标,搜寻附近的地点,建立地理位置索引可提高查询效率。
mongodb地理位置索引,2d和2dsphere,对应平面和球面。

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

use lbs;  db.lbs.insert(   {     loc:{       type: "Point",       coordinates: [113.332264, 23.156206]     },     name: "广州东站"   } )  db.lbs.insert(   {     loc:{       type: "Point",       coordinates: [113.330611, 23.147234]     },     name: "林和西"   } )  db.lbs.insert(   {     loc:{       type: "Point",       coordinates: [113.328095, 23.165376]     },     name: "天平架"   } )

2.创建地理位置索引

db.lbs.ensureIndex(   {     loc: "2dsphere"   } )

3.查询附近的坐标
当前位置为:时代广场,
坐标:

113.323568, 23.146436

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

db.lbs.find(   {     loc: {       $near:{         $geometry:{           type: "Point",           coordinates: [113.323568, 23.146436]         },         $maxDistance: 1000       }     }   } )

搜寻结果:

<br />

{ "_id" : ObjectId("556a651996f1ac2add8928fa"), "loc" : { "type" : "Point", "coordinates" : [ 113.330611, 23.147234 ] }, "name" : "林和西" }

php代码如下:

<?php // 连接mongodb function conn($dbhost, $dbname, $dbuser, $dbpasswd){   $server = 'mongodb://'.$dbuser.':'.$dbpasswd.'@'.$dbhost.'/'.$dbname;   try{     $conn = new MongoClient($server);     $db = $conn->selectDB($dbname);   } catch (MongoException $e){     throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);   }   return $db; }  // 插入坐标到mongodb function add($dbconn, $tablename, $longitude, $latitude, $name){   $index = array('loc'=>'2dsphere');   $data = array(       'loc' => array(           'type' => 'Point',           'coordinates' => array(doubleval($longitude), doubleval($latitude))       ),       'name' => $name   );   $coll = $dbconn->selectCollection($tablename);   $coll->ensureIndex($index);   $result = $coll->insert($data, array('w' => true));   return (isset($result['ok']) && !empty($result['ok'])) ? true : false; }  // 搜寻附近的坐标 function query($dbconn, $tablename, $longitude, $latitude, $maxdistance, $limit=10){   $param = array(     'loc' => array(       '$nearSphere' => array(         '$geometry' => array(           'type' => 'Point',           'coordinates' => array(doubleval($longitude), doubleval($latitude)),          ),         '$maxDistance' => $maxdistance*1000       )     )   );    $coll = $dbconn->selectCollection($tablename);   $cursor = $coll->find($param);   $cursor = $cursor->limit($limit);      $result = array();   foreach($cursor as $v){     $result[] = $v;   }     return $result; }  $db = conn('localhost','lbs','root','123456');  // 随机插入100条坐标纪录 for($i=0; $i<100; $i++){   $longitude = '113.3'.mt_rand(10000, 99999);   $latitude = '23.15'.mt_rand(1000, 9999);   $name = 'name'.mt_rand(10000,99999);   add($db, 'lbs', $longitude, $latitude, $name); }  // 搜寻一公里内的点 $longitude = 113.323568; $latitude = 23.146436; $maxdistance = 1; $result = query($db, 'lbs', $longitude, $latitude, $maxdistance); print_r($result<i>*本5文来源gaodai$ma#com搞$$代**码)网@</i><img>搞代码gaodaima</img>); ?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP地理位置搜寻并计算距离

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址