mongodb 地理位置搜尋

WBOY
發布: 2016-08-08 09:23:26
原創
1082 人瀏覽過

LBS,儲存每個地點的經緯度座標,搜尋附近的地點,建立地理位置索引可提高查詢效率。

mongodb地理位置索引,2d2dsphere,對應平面和球面。

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.查詢附近的地理位置索引
db.lbs.find(
    {
        loc: {
            $near:{
                $geometry:{
                    type: "Point",
                    coordinates: [113.323568, 23.146436]
                },
                $maxDistance: 1000
            }
        }
    }
)
登入後複製

3.查詢附近的地理位置索引

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

3.查詢附近的地理位置索引

<?php
// 连接mongodb
function conn($dbhost, $dbname, $dbuser, $dbpasswd){
    $server = &#39;mongodb://&#39;.$dbuser.&#39;:&#39;.$dbpasswd.&#39;@&#39;.$dbhost.&#39;/&#39;.$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 = &#39;113.3&#39;.mt_rand(10000, 99999);
    $latitude = &#39;23.15&#39;.mt_rand(1000, 9999);
    $name = &#39;name&#39;.mt_rand(10000,99999);
    add($db, &#39;lbs&#39;, $longitude, $latitude, $name);
}

// 搜寻一公里内的点
$longitude = 113.323568;
$latitude = 23.146436;
$maxdistance = 1;
$result = query($db, &#39;lbs&#39;, $longitude, $latitude, $maxdistance);
print_r($result);
?>
登入後複製

3.查詢附近的地理位置索引

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

db.auth(
    {
        "user":"root",
        "pwd":"123456"
    }
)
登入後複製

3.查詢附近的地理位置索引
rrreee

5
座標:113.323568, 23.146436

搜尋附近一公里內的點,由近到遠一點 rrreee


示範php程式碼,首先需要在

mongodb的lbs中建立使用者和執行auth

。方法如下:

rrreee🎜 🎜 以上就介紹了mongodb 地理位置搜尋,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。 🎜 🎜 🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!