imokhles/map-drawing-field-for-backpack

使用 Google Map API v3 为 Laravel 的 Backpack 管理面板绘制坐标并轻松保存到数据库

1.0 2021-10-12 07:02 UTC

This package is auto-updated.

Last update: 2024-09-13 09:28:50 UTC


README

Latest Version on Packagist Total Downloads

此包为 Backpack for Laravel 管理面板提供了一种 Map Drawing 字段类型。该 Map Drawing 字段允许管理员在地图上直接 绘制特定区域的坐标。它使用 Google Map (Drawing) API V3

视频

Enregistrement.de.l.ecran.2021-10-12.a.08.54.55.mov

需求

使用方法(多边形示例)

  • 安装 Laravel MySQL Spatial 扩展 后编辑您的模型
  • 在您的模型中使用 SpatialTrait
  • $spatialFields 中添加您的区域列名称
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Zone extends Model
{
    // You need to use SpatialTrait
    use HasFactory, SoftDeletes, SpatialTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'is_active',
    ];
    
    // area's column name
    protected $spatialFields = [
        'coordinates'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'is_active' => 'boolean',
    ];
}
  • 编辑您的 xxxCrudController
  • 导入 LineStringPointPolygon
use Grimzy\LaravelMysqlSpatial\Types\LineString;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
  • 重写 CreateOperation'sUpdateOperation'sstoreupdate 函数以在保存之前重新格式化数据
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }

    public function store()
    {
        $this->crud->setRequest($this->crud->validateRequest());
        $req = $this->crud->getRequest();

        // do something before validation, before save, before everything
        $this->crud->setRequest($req);
        $this->crud->unsetValidation(); // validation has already been run
        $response = $this->traitStore();
        // do something after save
        $this->handleCoords($req, $this->crud->getCurrentEntry());
        return $response;
    }

    public function update()
    {
        $this->crud->setRequest($this->crud->validateRequest());
        $req = $this->crud->getRequest();

        // do something before validation, before save, before everything
        $this->crud->setRequest($req);
        $this->crud->unsetValidation(); // validation has already been run
        $response = $this->traitUpdate();
        // do something after save
        $this->handleCoords($req, $this->crud->getCurrentEntry());

        return $response;
    }

    /**
     * @param $request
     * @param Zone $item
     */
    protected function handleCoords($request, Zone $item) {
        $value = $request->coordinates;
        foreach(explode('),(',trim($value,'()')) as $index=>$single_array){
            if($index == 0)
            {
                $lastcord = explode(',',$single_array);
            }
            $coords = explode(',',$single_array);
            $polygon[] = new Point($coords[0], $coords[1]);
        }

        $polygon[] = new Point($lastcord[0], $lastcord[1]);
        $item->coordinates = new Polygon([new LineString($polygon)]);
        $item->save();
    }

安装

通过 Composer

composer require imokhles/map-drawing-field-for-backpack

使用方法

在您的自定义 CrudController 中

$this->crud->addField([
    'name' => 'coordinates',
    'label' => 'Coordinates',
    'type' => 'map-drawing',
    'default_lat' => 30.193000747841246, // default latitude
    'default_lng' => 31.139526309011586, // default longitude
    'api_key' => 'GOOGLE_MAP_API_KEY',
    'view_namespace' => 'map-drawing-field-for-backpack::fields',
]);

注意 view_namespace 属性 - 确保它完全如上所述,以便告诉 Backpack 从此 插件包 加载字段,而不是假设它在 Backpack\CRUD 包内。

变更日志

请参阅 变更日志 了解最近更改的详细信息。

贡献

请参阅 contributing.md 了解详细信息和待办事项列表。

安全

如果您发现任何与安全相关的问题,请通过电子邮件联系 作者,而不是使用问题跟踪器。

致谢

许可

MIT。有关更多信息,请参阅 许可文件