PHP code example of lbcdev / filament-maps-widgets

1. Go to this page and download the library: Download lbcdev/filament-maps-widgets 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-maps-widgets example snippets


namespace App\Filament\Widgets;

use LBCDev\FilamentMapsWidgets\Widgets\MapWidget;
use LBCDev\MapGeometries\Marker;
use LBCDev\MapGeometries\MarkerCollection;

class LocationsMap extends MapWidget
{
    protected function getMarkers(): MarkerCollection
    {
        $markers = new MarkerCollection();
        
        $markers->add(
            Marker::make('home')
                ->lat(40.7128)
                ->lng(-74.0060)
                ->popup('New York City')
        );
        
        return $markers;
    }
    
    protected function getMapCenter(): array
    {
        return ['lat' => 40.7128, 'lng' => -74.0060];
    }
}

use LBCDev\FilamentMapsWidgets\Widgets\MapWidget;
use LBCDev\MapGeometries\Marker;

class MyMap extends MapWidget
{
    protected function getMarkers(): array
    {
        return [
            Marker::make('marker-1')
                ->lat(51.505)
                ->lng(-0.09)
                ->popup('London'),
        ];
    }
}

class MyMap extends MapWidget
{
    protected function getMapCenter(): array
    {
        return ['lat' => 51.505, 'lng' => -0.09];
    }
    
    protected function getMapZoom(): int
    {
        return 13;
    }
    
    protected function getMapOptions(): array
    {
        return [
            'scrollWheelZoom' => true,
            'dragging' => true,
            'minZoom' => 10,
            'maxZoom' => 18,
        ];
    }
}

use App\Models\Location;

class LocationsMap extends MapWidget
{
    protected function getMarkers(): MarkerCollection
    {
        $markers = new MarkerCollection();
        
        Location::all()->each(function ($location) use ($markers) {
            $markers->add(
                Marker::make($location->id)
                    ->lat($location->latitude)
                    ->lng($location->longitude)
                    ->color($location->is_active ? 'green' : 'red')
                    ->popup("<b>{$location->name}</b><br>{$location->address}")
            );
        });
        
        return $markers;
    }
}

use LBCDev\FilamentMapsWidgets\Actions\ZoomAction;

class MyMap extends MapWidget
{
    protected function getActions(): array
    {
        return [
            ZoomAction::make()
                ->position('topright'),
        ];
    }
}

return [
    'default_center' => [
        'lat' => 0,
        'lng' => 0,
    ],
    'default_zoom' => 10,
    'default_height' => '500px',
    'map_options' => [
        'scrollWheelZoom' => true,
        'dragging' => true,
        // ... more options
    ],
];

class FilteredMap extends MapWidget
{
    public array $filters = [];
    
    protected $listeners = [
        'filtersUpdated' => 'handleFiltersUpdated',
    ];
    
    public function mount(array $filters = []): void
    {
        $this->filters = $filters;
        parent::mount();
    }
    
    protected function getMarkers(): MarkerCollection
    {
        // Apply filters to your query
        $query = Location::query();
        
        if ($this->filters['category'] ?? null) {
            $query->where('category_id', $this->filters['category']);
        }
        
        // Build markers from filtered results
        // ...
    }
    
    public function handleFiltersUpdated(array $filters): void
    {
        $this->filters = $filters;
        $this->refresh();
    }
}

class MyMap extends MapWidget
{
    public string $height = '700px';
    
    protected bool $hasBorder = true;
}
bash
php artisan vendor:publish --tag="filament-maps-widgets-config"