当前位置:网站首页>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);
边栏推荐
- Standard ideas for the study of tumor heterogeneity (multiomics + multipoint sampling)
- 【牛客网SQL篇】非技术快速入门
- Shell command output
- Detailed explanation of initial order of pointer
- Go language type conversion
- Shell uppercase to lowercase
- Theoretical basis and underlying technology research materials of audio and video
- ora-02097 ora-00093
- Simple understanding process of machine learning, basic steps of face recognition
- 音視頻的理論基礎與底層技術研究資料
猜你喜欢

Laravel基于MongoDB(LBS)实现附近的人、店铺、房源

Presto RBO之谓词下推

The best practice of knowledge management in the digital age

Take a data catalog architecture upgrade as an example to talk about the performance optimization of the business system

Blazorise scenario to build high-performance web applications

Web slider drag selection value slider plug-in

After class assignment of module 3 of the construction practice camp

openEuler笔记本联网

一次SQL查询优化原理分析:900W+数据,从17s到300ms

Simple understanding process of machine learning, basic steps of face recognition
随机推荐
[plotly quick start] I have drawn several exquisite charts with plotly, which is beautiful!!
Vector底层实现(常用方法)
Renewal certificate of kubernetes kubeadm Management Certificate
Laravel框架中如何使用MongoDB数据库
Flat login form page
User defined annotation @interface usage understanding - personal notes
2022dasctf APR x fat epidemic prevention challenge warmup PHP
Presto RBO之谓词下推
Oracle index status query and index reconstruction
Simple understanding process of machine learning, basic steps of face recognition
Assembly language integrated development environment learning notes
GDB notes (10) - check for memory leak, heap overflow, stack overflow, global memory overflow, and continue using after release
Is it safe for Huishang futures to open an account online? Are there any account opening guidelines?
shell 上移目录
Embedded exam review
About concurrency and parallelism, are the fathers of go and Erlang wrong?
[JS] pseudo array to array
数论 --- 朴素筛法、埃氏筛法、线性筛法
shell 微调加载器
jenkins根据凭证ID查看忘记的凭证密码以及重置admin密码的操作方法