国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現搜索地理位置及計算兩點地理位置間距離的實例

2024-05-04 23:42:05
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP實現搜索地理位置及計算兩點地理位置間距離的實例,地理位置搜尋的例子中使用到了MongoDB數據庫,需要的朋友可以參考下
 

地理位置搜尋
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       }     }   } ) 

搜尋結果:

 

復制代碼代碼如下:

 

{ "_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); ?> 

演示php代碼,首先需要在mongodb的lbs中創建用戶和執行auth。方法如下:

use lbs; db.createUser(   {     "user":"root",     "pwd":"123456",     "roles":[]   } )  db.auth(   {     "user":"root",     "pwd":"123456"   } ) 

 

計算兩點地理坐標的距離
功能:根據圓周率和地球半徑系數與兩點坐標的經緯度,計算兩點之間的球面距離。


獲取兩點坐標距離:

<?php/** * 計算兩點地理坐標之間的距離 * @param Decimal $longitude1 起點經度 * @param Decimal $latitude1 起點緯度 * @param Decimal $longitude2 終點經度  * @param Decimal $latitude2 終點緯度 * @param Int   $unit    單位 1:米 2:公里 * @param Int   $decimal  精度 保留小數位數 * @return Decimal */function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){  $EARTH_RADIUS = 6370.996; // 地球半徑系數  $PI = 3.1415926;  $radLat1 = $latitude1 * $PI / 180.0;  $radLat2 = $latitude2 * $PI / 180.0;  $radLng1 = $longitude1 * $PI / 180.0;  $radLng2 = $longitude2 * $PI /180.0;  $a = $radLat1 - $radLat2;  $b = $radLng1 - $radLng2;  $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));  $distance = $distance * $EARTH_RADIUS * 1000;  if($unit==2){    $distance = $distance / 1000;  }  return round($distance, $decimal);}// 起點坐標$longitude1 = 113.330405;$latitude1 = 23.147255;// 終點坐標$longitude2 = 113.314271;$latitude2 = 23.1323;$distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 1);echo $distance.'m'; // 2342.38m$distance = getDistance($longitude1, $latitude1, $longitude2, $latitude2, 2);echo $distance.'km'; // 2.34km?>
 


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 民勤县| 始兴县| 修武县| 江陵县| 西平县| 连南| 三穗县| 上高县| 台中县| 股票| 营口市| 湖南省| 九江市| 吴川市| 富阳市| 仪征市| 沐川县| 深水埗区| 临西县| 平安县| 漠河县| 民勤县| 安庆市| 略阳县| 阳谷县| 宜君县| 永定县| 长武县| 项城市| 高唐县| 舟山市| 崇仁县| 依安县| 鸡西市| 三江| 永嘉县| 上栗县| 临颍县| 平泉县| 长海县| 平潭县|