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');
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');
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(),
]);
}
}