当前位置:网站首页>Laravel implements nearby people, shops and houses based on mongodb (LBS)

Laravel implements nearby people, shops and houses based on mongodb (LBS)

2022-06-09 01:21:00 Life goes on and battles go on

Premise PHP Need to install MongoDb Expand

One 、Laravel install mongodb Expand

composer require jenssegers/mongodb ^3.6 -vvv

Two 、 The controller method populates some test data

$res = DB::connection('mongodb')
    ->table('t2')
    ->insert( [
        'name'=>'juejin',
        'loc' => [
            'longitude' => 116.48105 ,
            'latitude' =>39.996794,
        ],
    ]);

$res = DB::connection('mongodb')
    ->table('t2')
    ->insert( [
        'name'=>'ireader',
        'loc' => [
            'longitude' => 116.514203 ,
            'latitude' =>39.905409,
        ],
    ]);

$res = DB::connection('mongodb')
    ->table('t2')
    ->insert( [
        'name'=>'meituan',
        'loc' => [
            'longitude' => 116.489033,
            'latitude' =>40.007669,
        ],
    ]);


$res = DB::connection('mongodb')
    ->table('t2')
    ->insert( [
        'name'=>'jd',
        'loc' => [
            'longitude' => 116.562108 ,
            'latitude' =>39.787602,
        ],
    ]);

$res = DB::connection('mongodb')
    ->table('t2')
    ->insert( [
        'name'=>'xiaomi',
        'loc' => [
            'longitude' => 116.334255 ,
            'latitude' =>40.027400,
        ],
    ]);

3、 ... and 、 If you want to do something nearby, you should set the index

Four 、 Encapsulate the nearby recommended service class

<?php

namespace App\Http\Controllers\Service;


use Illuminate\Support\Facades\DB;


class NearbySeller
{
    private $longitude;# longitude 
    private $latitude;# latitude 
    private $mongodb;

    /**
     *  initialization 
     *
     * @param string $collection  A collection that requires operations 
     * @param float $longitude  longitude 
     * @param float $latitude  latitude 
     */
    public function __construct($collection, $longitude, $latitude)
    {
        $this->longitude = (float)$longitude;
        $this->latitude = (float)$latitude;

        $this->mongodb = DB::connection('mongodb')->collection($collection);
    }

    /**
     *  Search how many km The information in , Return from near to far 
     * @param integer $kilometer  How much km The information in 
     * @param integer $perPage  How much data to get , each page 
     * @param integer $page  Get the page number of ,
     *
     * @return  Return the obtained address distance 
     */
    public function getRangeBySort($kilometer, $page, $limit)
    {
        $where = [
            'loc' => [
                '$nearSphere' => [
                    '$geometry' => [
                        'type' => 'Point',
                        'coordinates' => [$this->longitude, $this->latitude]
                    ],
                    '$maxDistance' => $kilometer*1000
                ]
            ],
        ];

        $list = $this->mongodb->whereRaw($where)->skip(($page-1) * $limit)->take($limit)->get();

        return $list;
    }

    /**
     *  Search how many km The information in , Return from near to far 
     * @param integer $kilometer  How much km The information in 
     *
     * @return  Return the obtained address distance 
     */
    public function getRadiusBydisorder($kilometer)
    {
        $where = [
            'loc' => [
                '$geoWithin' => [
                    '$centerSphere' => [
                        [
                            $this->longitude,
                            $this->latitude
                        ],
                        $kilometer/6371
                    ]
                ]
            ],
        ];

        $list = $this->mongodb->whereRaw($where)->get();

        return $list;
    }
}

5、 ... and 、 The controller method is called

$near = new NearbySeller('t2', 116.48105, 39.996794);
$page = $request->get('page') ?: 1;
$limit = 3;

//  Test how much km Store information within , List mode 
$list = $near->getRangeBySort(20, $page, $limit);

//  How many tests km Store information within the radius , Map mode 
$list = $near->getRadiusBydisorder(20);

原网站

版权声明
本文为[Life goes on and battles go on]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090116396750.html