PHP實(shí)現(xiàn)搜索地理位置及計(jì)算兩點(diǎn)地理位置間距離的實(shí)例
地理位置搜尋
LBS,存儲(chǔ)每個(gè)地點(diǎn)的經(jīng)緯度坐標(biāo),搜尋附近的地點(diǎn),建立地理位置索引可提高查詢(xún)效率。
mongodb地理位置索引,2d和2dsphere,對(duì)應(yīng)平面和球面。
1.創(chuàng)建lbs集合存放地點(diǎn)坐標(biāo)
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.創(chuàng)建地理位置索引
db.lbs.ensureIndex(
{
loc: "2dsphere"
}
)
3.查詢(xún)附近的坐標(biāo)
當(dāng)前位置為:時(shí)代廣場(chǎng),
坐標(biāo):
113.323568, 23.146436
搜尋附近一公里內(nèi)的點(diǎn),由近到遠(yuǎn)排序
db.lbs.find(
{
loc: {
$near:{
$geometry:{
type: "Point",
coordinates: [113.323568, 23.146436]
},
$maxDistance: 1000
}
}
}
)
搜尋結(jié)果:
{ "_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;
}
// 插入坐標(biāo)到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;
}
// 搜尋附近的坐標(biāo)
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');
// 隨機(jī)插入100條坐標(biāo)紀(jì)錄
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);
}
// 搜尋一公里內(nèi)的點(diǎn)
$longitude = 113.323568;
$latitude = 23.146436;
$maxdistance = 1;
$result = query($db, 'lbs', $longitude, $latitude, $maxdistance);
print_r($result);
?>
演示php代碼,首先需要在mongodb的lbs中創(chuàng)建用戶(hù)和執(zhí)行auth。方法如下:
use lbs;
db.createUser(
{
"user":"root",
"pwd":"123456",
"roles":[]
}
)
db.auth(
{
"user":"root",
"pwd":"123456"
}
)
計(jì)算兩點(diǎn)地理坐標(biāo)的距離
功能:根據(jù)圓周率和地球半徑系數(shù)與兩點(diǎn)坐標(biāo)的經(jīng)緯度,計(jì)算兩點(diǎn)之間的球面距離。
獲取兩點(diǎn)坐標(biāo)距離:
<?php
/**
* 計(jì)算兩點(diǎn)地理坐標(biāo)之間的距離
* @param Decimal $longitude1 起點(diǎn)經(jīng)度
* @param Decimal $latitude1 起點(diǎn)緯度
* @param Decimal $longitude2 終點(diǎn)經(jīng)度
* @param Decimal $latitude2 終點(diǎn)緯度
* @param Int $unit 單位 1:米 2:公里
* @param Int $decimal 精度 保留小數(shù)位數(shù)
* @return Decimal
*/
function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){
$EARTH_RADIUS = 6370.996; // 地球半徑系數(shù)
$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);
}
// 起點(diǎn)坐標(biāo)
$longitude1 = 113.330405;
$latitude1 = 23.147255;
// 終點(diǎn)坐標(biāo)
$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如何計(jì)算兩坐標(biāo)點(diǎn)之間的距離
- golang與php實(shí)現(xiàn)計(jì)算兩個(gè)經(jīng)緯度之間距離的方法
- PHP安裝GeoIP擴(kuò)展根據(jù)IP獲取地理位置及計(jì)算距離的方法
- php實(shí)現(xiàn)計(jì)算百度地圖坐標(biāo)之間距離的方法
- php計(jì)算兩個(gè)坐標(biāo)(經(jīng)度,緯度)之間距離的方法
- PHP計(jì)算百度地圖兩個(gè)GPS坐標(biāo)之間距離的方法
- PHP根據(jù)兩點(diǎn)間的經(jīng)緯度計(jì)算距離
- PHP計(jì)算2點(diǎn)經(jīng)緯度之間的距離代碼
- php兩點(diǎn)地理坐標(biāo)距離的計(jì)算方法
相關(guān)文章
yii2實(shí)現(xiàn)根據(jù)時(shí)間搜索的方法
這篇文章主要介紹了yii2實(shí)現(xiàn)根據(jù)時(shí)間搜索的方法,涉及Yii2時(shí)間的操作及數(shù)據(jù)查詢(xún)相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
基于thinkPHP3.2實(shí)現(xiàn)微信接入及查詢(xún)token值的方法
這篇文章主要介紹了基于thinkPHP3.2實(shí)現(xiàn)微信接入及查詢(xún)tooken值的方法,結(jié)合實(shí)例形式分析了thinkPHP整合微信接口操作token值查詢(xún)的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
這篇文章主要介紹了UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版的相關(guān)資料,需要的朋友可以參考下2015-12-12
ThinkPHP自定義函數(shù)解決模板標(biāo)簽加減運(yùn)算的方法
這篇文章主要介紹了ThinkPHP自定義函數(shù)解決模板標(biāo)簽加減運(yùn)算的方法,實(shí)例分析了ThinkPHP中自定義函數(shù)在模板標(biāo)簽中的使用技巧,需要的朋友可以參考下2015-07-07
PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法
下面小編就為大家?guī)?lái)一篇PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
php實(shí)現(xiàn)圖形顯示Ip地址的代碼及注釋
這篇文章主要介紹了php實(shí)現(xiàn)圖形顯示Ip地址,有需要的朋友可以參考一下2014-01-01

