PHP code example of lbcdev / filament-map-field

1. Go to this page and download the library: Download lbcdev/filament-map-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lbcdev / filament-map-field example snippets


    public function panel(Panel $panel): Panel{
        return $panel
            ...
            ->renderHook(
                'panels::head.end',
                fn(): string => view('filament.hooks.leaflet-assets')->render()
            )
            ...
    }

use Lbcdev\FilamentMapField\Forms\Components\MapField;

MapField::make('location')
    ->latitude('latitude')
    ->longitude('longitude');

MapField::make('location')
    ->latitude('latitude')      // Campo donde se guardará la latitud
    ->longitude('longitude')    // Campo donde se guardará la longitud
    ->height(500)              // Altura del mapa en píxeles (default: 400)
    ->zoom(15)                 // Nivel de zoom inicial (default: 15)
    ->showPasteButton()        // Mostrar botón para pegar coordenadas
    ->showLabel()              // Mostrar etiqueta con coordenadas
    ->interactive();           // Permitir interacción (default: true)

// Usando readOnly() - Compatible con la API estándar de Filament
MapField::make('location')
    ->latitude('latitude')
    ->longitude('longitude')
    ->readOnly();

// O usando interactive(false) - Mismo resultado
MapField::make('location')
    ->latitude('latitude')
    ->longitude('longitude')
    ->interactive(false);



namespace App\Filament\Resources;

use App\Models\Location;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Lbcdev\FilamentMapField\Forms\Components\MapField;

class LocationResource extends Resource
{
    protected static ?string $model = Location::class;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->
                            ->

// ✅ CORRECTO: make() usa el campo padre 'ubicacion'
MapField::make('ubicacion')
    ->latitude('ubicacion.latitud')
    ->longitude('ubicacion.longitud')
    ->columnSpanFull();

// ❌ INCORRECTO: make() usa 'map' pero los campos son 'ubicacion.latitud'
// Esto causará error "The ubicación field is 

class Store extends Model
{
    protected $fillable = ['name', 'ubicacion'];

    protected $casts = [
        'ubicacion' => 'array', // Campo JSON
    ];
}

Schema::create('stores', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->json('ubicacion')->nullable(); // Campo JSON
    $table->timestamps();
});

use Lbcdev\FilamentMapField\Infolists\Entries\MapEntry;

MapEntry::make('location')
    ->latitude('latitude')
    ->longitude('longitude');

MapEntry::make('location')
    ->latitude('latitude')
    ->longitude('longitude')
    ->height(400)
    ->zoom(15)
    ->showLabel();



namespace App\Filament\Resources;

use App\Models\Location;
use Filament\Infolists;
use Filament\Infolists\Infolist;
use Filament\Resources\Resource;
use Lbcdev\FilamentMapField\Infolists\Entries\MapEntry;

class LocationResource extends Resource
{
    protected static ?string $model = Location::class;

    public static function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Infolists\Components\TextEntry::make('name'),

                Infolists\Components\TextEntry::make('address'),

                Infolists\Components\Grid::make(2)
                    ->schema([
                        Infolists\Components\TextEntry::make('latitude')
                            ->numeric(decimalPlaces: 6),

                        Infolists\Components\TextEntry::make('longitude')
                            ->numeric(decimalPlaces: 6),
                    ]),

                MapEntry::make('map')
                    ->latitude('latitude')
                    ->longitude('longitude')
                    ->height(400)
                    ->zoom(15)
                    ->columnSpanFull(),
            ]);
    }
}

use Lbcdev\FilamentMapField\Forms\Components\MapBoundsField;

MapBoundsField::make('area')
    ->southWestLat('sw_lat')
    ->southWestLng('sw_lng')
    ->northEastLat('ne_lat')
    ->northEastLng('ne_lng');

MapBoundsField::make('area')
    ->southWestLat('sw_lat')        // Campo para latitud suroeste
    ->southWestLng('sw_lng')        // Campo para longitud suroeste
    ->northEastLat('ne_lat')        // Campo para latitud noreste
    ->northEastLng('ne_lng')        // Campo para longitud noreste
    ->height(500)                   // Altura del mapa en píxeles
    ->zoom(13)                      // Nivel de zoom inicial
    ->showLabel()                   // Mostrar etiqueta con coordenadas
    ->defaultCenter(40.4168, -3.7038); // Centro por defecto (Madrid)

// ✅ CORRECTO: make() usa el campo padre 'bounds'
MapBoundsField::make('bounds')
    ->southWestLat('bounds.sw_lat')
    ->southWestLng('bounds.sw_lng')
    ->northEastLat('bounds.ne_lat')
    ->northEastLng('bounds.ne_lng')
    ->height(500)
    ->zoom(13);

use Lbcdev\FilamentMapField\Infolists\Entries\MapBoundsEntry;

MapBoundsEntry::make('area')
    ->southWestLat('sw_lat')
    ->southWestLng('sw_lng')
    ->northEastLat('ne_lat')
    ->northEastLng('ne_lng');

MapBoundsEntry::make('area')
    ->southWestLat('sw_lat')
    ->southWestLng('sw_lng')
    ->northEastLat('ne_lat')
    ->northEastLng('ne_lng')
    ->height(400)
    ->zoom(13)
    ->showLabel();

// Ejemplo 1: MapField con validación requerida
MapField::make('ubicacion')
    ->latitude('ubicacion.latitud')
    ->longitude('ubicacion.longitud')
    ->height(500)
    ->zoom(15)
    ->showPasteButton()
    ->stLat('limites.latitud_max')
    ->northEastLng('limites.longitud_max')
    ->height(500)
    ->zoom(13)
    ->0.000001),

        Forms\Components\TextInput::make('longitude')
            ->numeric()
            ->

Forms\Components\Tabs::make('Locations')
    ->tabs([
        Forms\Components\Tabs\Tab::make('Origen')
            ->schema([
                Forms\Components\Grid::make(2)
                    ->schema([
                        Forms\Components\TextInput::make('origin_latitude')
                            ->numeric()
                            -> ->height(400)
                    ->showPasteButton(),
            ]),

        Forms\Components\Tabs\Tab::make('Destino')
            ->schema([
                Forms\Components\Grid::make(2)
                    ->schema([
                        Forms\Components\TextInput::make('destination_latitude')
                            ->numeric()
                            ->

// Migración
Schema::create('locations', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->decimal('latitude', 10, 8)->nullable();
    $table->decimal('longitude', 11, 8)->nullable();
});

// Formulario
MapField::make('map')
    ->latitude('latitude')
    ->longitude('longitude');

// Resultado en BD:
// latitude: 40.416775
// longitude: -3.703790

// Migración
Schema::create('locations', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->json('ubicacion')->nullable();
});

// Modelo
class Location extends Model
{
    protected $casts = [
        'ubicacion' => 'array',
    ];
}

// Formulario
MapField::make('ubicacion')
    ->latitude('ubicacion.latitud')
    ->longitude('ubicacion.longitud')
    ->height(500)
    ->zoom(15)
    ->showPasteButton();

// Resultado en BD (campo JSON):
// ubicacion: {"latitud": "40.416775", "longitud": "-3.703790"}



namespace App\Filament\Resources;

use App\Models\Store;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Lbcdev\FilamentMapField\Forms\Components\MapField;

class StoreResource extends Resource
{
    protected static ?string $model = Store::class;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->columnSpanFull(),
            ]);
    }
}
bash
php artisan filament:cache-components
php artisan view:clear
php artisan cache:clear