zoukankan      html  css  js  c++  java
  • PHP使用MongoDB存储经纬度,查询距离

    https://blog.csdn.net/qq_40012295/article/details/84861466

    https://docs.mongodb.com/manual/reference/command/geoNear/index.html

    https://juejin.im/entry/5b7cfe296fb9a019d80a8ed8

    <?php
    
    /*
    使用命令创建数据库:
    use user
    
    使用命令创建集合:
    db.createCollection(user)
    
    使用命令创建2dsphere索引:
    db.user.createIndex({location: "2dsphere"})
    */
    
    //PHP代码插入经纬度数据:
    function uploadMongoDBLocation()
    {
        $document = [
            'name' => '张三',
            'location' => [
                (float)115.036545,
                (float)36.313916,
            ],
        ];
        $manager = new MongoDBDriverManager('mongodb://127.0.0.1:27017');
        $bulk = new MongoDBDriverBulkWrite;
        $bulk->insert($document); // 可连续使用多个insert
        $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
        $res = $manager->executeBulkWrite('user.user', $bulk, $writeConcern);
        return $res;
    }
    
    //PHP代码查询距离:
    //注意:这里使用的command根据版本不同,用法可能会有区别,这里使用的MongoDB版本是3.6.5
    function findMongoDBLocation()
    {
        $document = [
            'geoNear' => 'user',
            'near' => [
                'type' => 'Point',
                'coordinates' => [(float)'115.042725', (float)'36.312956'],
            ],
            'spherical' => true,
            'minDistance' => 0,
            'maxDistance' => 100000,
            'num' => 1000
        ];
        $command = new MongoDBDriverCommand($document);
        $manager = new MongoDBDriverManager('mongodb://127.0.0.1:27017');
        $cursor = $manager->executeCommand('user', $command);
        $items = [];
        foreach ($cursor as $document) {
            $total = count($document->results);
            if ($total > 0) {
                foreach ($document->results as $result) {
                    $item = json_decode(json_encode($result->obj), true);
                    $item['distance'] = intval($result->dis);
                    unset($item['_id']);
                    $items[] = $item;
                }
            }
        }
        return $items;
    }

    https://zhuanlan.zhihu.com/p/51839804

    MongoDB实现附近的人

    往数据库中批量插入数据,use mage切换到mage数据库,执行db.user.insertMany(),user是文档名,insertMany()是批量插入命令,里面传入json数组,
    {'name':'杨帅哥', 'address':'江西省南昌市青山湖区市场和质量监督管理局', 'gender':1, loc:[115.993121,28.676436]}
    代表一条用户数据,其中gender:0代表女1,代表男,loc是一个经纬度的数组,当然也可以是loc : { lng : 115.993067 , lat : 28.67606 },但官方推荐数组。 db.user.insertMany([ {'name':'杨帅哥', 'address':'江西省南昌市青山湖区市场和质量监督管理局', 'gender':1, loc:[115.993121,28.676436]}, {'name':'王美眉', 'address':'江西省南昌市青山湖区创新一路职位小厨', 'gender':0, loc:[116.000093,28.679402]}, {'name':'张美眉', 'address':'江西省南昌市青山湖区紫阳大道1916号', 'gender':0, loc:[115.999967,28.679743]}, {'name':'李美眉', 'address':'江西省南昌市青山湖区云中城', 'gender':0, loc:[115.995593,28.681632]}, {'name':'彭美眉', 'address':'江西省南昌市青山湖区北京东路1666号', 'gender':0, loc:[115.975543,28.679509]}, {'name':'赵美眉', 'address':'江西省南昌市青山湖区市场一路大润发', 'gender':0, loc:[115.968428,28.669368]}, {'name':'廖美眉', 'address':'江西省南昌市南昌县奥林匹克中心', 'gender':0, loc:[116.035262,28.677037]}, {'name':'余帅哥', 'address':'江西省南昌市南昌县科技学院瑶湖校区', 'gender':1, loc:[116.02477,28.68667]}, {'name':'吴帅哥', 'address':'江西省南昌市青山湖区创新一路母婴店', 'gender':1, loc:[116.002384,28.683865]}, {'name':'何帅哥', 'address':'江西省南昌市青山湖区紫阳大道2999号', 'gender':1, loc:[116.000821,28.68129]}, ])

    设置2d索引

    因为我以二维平面上点的方式存储的数据,想要进行LBS查询,那么要设置2d索引。db.user.createIndex({'loc':"2d"})其中loc是索引的字段。

    六、查询附近200米的人

    查询附近的人,首先的指导当前用户所在的经纬度,如果不仅想要得到数据还要得到距离,那么可以使用$geoNear指令,如果距离自己去计算可以使用$near或者$geoWithin然后在手动计算距离。此处采用$geoNear指令查询附近2000m的人。

    db.user.aggregate({
        $geoNear:{
            near: [115.999567,28.681813], // 当前坐标
            spherical: true, // 计算球面距离
            distanceMultiplier: 6378137, // 地球半径,单位是米,那么的除的记录也是米
            maxDistance: 2000/6378137, // 过滤条件2000米内,需要弧度
            distanceField: "distance" // 距离字段别名
        }
    })
        public function CI_mongodb($data)
        {
            //near: [115.999567,28.681813], // 当前坐标
            //spherical: true, // 计算球面距离
            //distanceMultiplier: 6378137, // 地球半径,单位是米,那么的除的记录也是米
            //maxDistance: 400/6378137, // 过滤条件2000米内,需要弧度
            //distanceField: "distance" // 距离字段别名
    
            $document = [
                'geoNear' => 'user',
                'near' => [(float)'115.999567', (float)'28.681813'],
                'spherical' => true,
                'distanceMultiplier' => 6378137,
                'maxDistance' => 400 / 6378137,
                'num' => 2,
            ];
            $res = $this>CI>lib_mongodb>command($document);
            PE($res);
        
        }
  • 相关阅读:
    idea创建项目报错(Maven execution terminated abnormally (exit code 1) )解决方案
    mysql连接查询
    mysql特殊使用
    eclipse发布web
    eclipse启动web应用 报错
    hdu 2018
    atom安装插件失败 latex
    牛腩新闻发布系统——解惑:VS2012验证码加载不出来
    IIS的安装和配置
    InoReader——网页无法打开
  • 原文地址:https://www.cnblogs.com/rxbook/p/10954762.html
Copyright © 2011-2022 走看看