imokhles/map-drawing-field-for-backpack

Draw coordinates and save the values easily to your database using Google Map API v3 for Backpack for Laravel

1.0 2021-10-12 07:02 UTC

This package is auto-updated.

Last update: 2024-04-13 08:37:47 UTC


README

Latest Version on Packagist Total Downloads

This package provides a Map Drawing field type for the Backpack for Laravel administration panel. The Map Drawing field allows admins to draw coordinates for specific areas on the map directly. It uses Google Map (Drawing) API V3.

Video

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

Requirements

How to use ( Polygon Example )

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',
    ];
}
  • Edit your xxxCrudController
  • Import LineString, Point, Polygon
use Grimzy\LaravelMysqlSpatial\Types\LineString;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
  • Overwrite CreateOperation's and UpdateOperation's store and update functions to reformat the data before saving it
    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();
    }

Installation

Via Composer

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

Usage

Inside your custom 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',
]);

Notice the view_namespace attribute - make sure that is exactly as above, to tell Backpack to load the field from this addon package, instead of assuming it's inside the Backpack\CRUD package.

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email the author instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.