似梦似醒

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
composer require jenssegers/mongodb

jenssegers/mongodb 官方文档地址
mongoGeo 官方文档说明
百度坐标拾取器

config/database.php 添加配置项

'mongodb' => [
    'driver' => 'mongodb',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE', 'homestead'),
    'username' => env('DB_USERNAME', 'homestead'),
    'password' => env('DB_PASSWORD', 'secret'),
    'options' => [
        // here you can pass more settings to the Mongo Driver Manager
        // see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use

        'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), // required with Mongo 3+
    ],
],

创建模型

<?php


namespace App\Models\Mongo;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class ShopsMg extends Eloquent
{
    protected $connection = 'mongodb';  //选择数据库类型
    protected $collection = 'shops';     //文档名
    protected $primaryKey = '_id';    //设置id
    //protected $casts = ["shop_id"=>"integer"]; //设置字段类型
    protected $guarded = [];  //取消白名单

}

必须先了解mongo中地理位置GeoJson类型有哪些
注意点的搜索必须建立 2dsphere 索引。 一定要仔细去看mongodbGeo 官方文档
查找坐标附近的点

$bars = Bar::where('location', 'near', [
    '$geometry' => [
        'type' => 'Point', //类型为点
        'coordinates' => [ //坐标
            -0.1367563, // 经度
            51.5100913, // 纬度
        ],
    ],
    '$maxDistance' => 50, //最大搜索半径 ,单位 m
])->get();

在指定地理空间中的文档

$bars = Bar::where('location', 'geoWithin', [
    '$geometry' => [
        'type' => 'Polygon', //指定的地理空间
        'coordinates' => [   //坐标
            [
                [-0.1450383, 51.5069158],
                [-0.1367563, 51.5100913],
                [-0.1270247, 51.5013233],
                [-0.1450383, 51.5069158],
            ],
        ],
    ],
])->get();

查找可以匹配到的多边形

$bars = Bar::where('location', 'geoIntersects', [
    '$geometry' => [
        'type' => 'LineString', //GeoJson 对象
        'coordinates' => [ //坐标
            [-0.144044, 51.515215],
            [-0.129545, 51.507864],
        ],
    ],
])->get();

字段设计

门店id :shop_id

门店名称:shop_name

门店坐标 : shop_coordinates

配送类型: delivery_type “211” or “29”

电子围栏类型: scope_type 圆形:circle。多边形:polygon

半径: radius 单位米,

多边形坐标: polygon_coordinates

一些测试数据

use jdjsj
# 1店 圆 211 朝阳医院 
将200修改为200, 就有覆盖关系了,可以搜索最近

db.shops.insert({
	shop_id: 1, 
	shop_name: "朝阳医院",
    shop_coordinates: [116.460107,39.931231],
    delivery_type:"eleven",
    scope_type:"circle",
    radius: 200,
    polygon_coordinates: [],
})

# 2店 多边形 211 团结湖公园
db.shops.insert({
	shop_id: 2, 
	shop_name: "团结湖公园",
    shop_coordinates: [116.470923,39.931425],
    delivery_type:"eleven",
    scope_type:"polygon",
    radius: 0,
    polygon_coordinates: {
     type:"Polygon",
     coordinates:[
     [
        [116.468731,39.933029],
        [116.469019,39.929488],
        [116.472648,39.929958],
        [116.468731,39.933029]
    ]
    ]},
})

# 3店 多边形 211 好世界商业广场
db.shops.insert({
	shop_id: 3, 
	shop_name: "好世界商业广场",
    shop_coordinates: [116.469522,39.927026],
    delivery_type:"eleven",
    scope_type:"polygon",
    radius: 0,
    polygon_coordinates: {
     type:"Polygon",
     coordinates:[
     [
    [116.466288,39.928575],
    [116.472935,39.928354],
    [116.472935,39.924647],
    [116.46636,39.925172],
    [116.466288,39.928575]
    ]
    ]},
})

# 4店 圆 211 新城国际和以太广场中间的

db.shops.insert({
	shop_id: 4, 
	shop_name: "新城国际",
    shop_coordinates: [116.462192,39.922212],
    delivery_type:"eleven",
    scope_type:"circle",
    radius: 200,
    polygon_coordinates: [],
})

db.shops.createIndex({"shop_coordinates":"2dsphere"})
posted on 2021-11-22 14:34  人生如梦,梦如人生  阅读(229)  评论(0编辑  收藏  举报