PHP code example of theabhishekin / filament-location

1. Go to this page and download the library: Download theabhishekin/filament-location 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/ */

    

theabhishekin / filament-location example snippets


return [
    'google_maps_api_key' => env('GOOGLE_MAPS_API_KEY'),
    'default_zoom' => 15,
    'map_height' => '400px',
    'enable_street_view' => true,
    'map_type' => 'standard', // standard, satellite, hybrid, terrain
    'location_accuracy' => 100,
    'location_timeout' => 10000,
    'enable_high_accuracy' => true,
    'map_controls' => [
        'zoom_control' => true,
        'map_type_control' => true,
        'scale_control' => true,
        'street_view_control' => true,
        'rotate_control' => true,
        'fullscreen_control' => true,
    ],
];

Schema::table('users', function (Blueprint $table) {
    $table->json('location')->nullable();
    $table->timestamp('location_updated_at')->nullable();
});



namespace App\Models;

use TheAbhishekIN\FilamentLocation\Concerns\HasLocation;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasLocation;

    protected $fillable = [
        'name',
        'email',
        'location',
        'location_updated_at',
    ];

    protected $casts = [
        'location' => 'array',
        'location_updated_at' => 'datetime',
    ];
}



namespace App\Filament\Resources;

use App\Models\User;
use TheAbhishekIN\FilamentLocation\Forms\Components\LocationPicker;
use TheAbhishekIN\FilamentLocation\Tables\Columns\LocationColumn;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = 'heroicon-o-users';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->ponents\DateTimePicker::make('location_updated_at')
                    ->label('Location Updated At')
                    ->disabled(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('email')
                    ->searchable()
                    ->sortable(),

                LocationColumn::make('location')
                    ->label('Location')
                    ->iconType('home')
                    ->iconSize('lg')
                    ->iconColor('primary')
                    ->title('User Location')
                    ->showTooltip(true)
                    ->tooltipText('Click to view location on map')
                    ->getLatitudeUsing(fn($record) => $record->location['latitude'] ?? null)
                    ->getLongitudeUsing(fn($record) => $record->location['longitude'] ?? null),

                Tables\Columns\TextColumn::make('location_updated_at')
                    ->label('Location Updated')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\Filter::make('has_location')
                    ->label('Has Location')
                    ->query(fn($query) => $query->whereNotNull('location'))
                    ->toggle(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
            'create' => Pages\CreateUser::route('/create'),
            'edit' => Pages\EditUser::route('/{record}/edit'),
        ];
    }
}

LocationPicker::make('location')
    ->label('Select Location')
    ->ht('400px')             // Map container height
    ->mapType('standard')         // standard, satellite, hybrid, terrain
    ->showCoordinates(true)       // Show latitude/longitude coordinates
    ->showMap(true)               // Show/hide the map
    ->mapControls([               // Configure map controls
        'zoom_control' => true,
        'street_view_control' => false,
    ])
    ->helperText('Click on the map to select a location');

LocationColumn::make('location')
    ->label('Location')
    ->iconType('home')                    // home, office, check-in, check-out, travel
    ->iconSize('lg')                      // sm, md, lg, xl
    ->iconColor('primary')                // primary, success, warning, danger
    ->title('Location Details')           // Modal title
    ->showTooltip(true)                   // Show hover tooltip
    ->tooltipText('View on map')          // Custom tooltip text
    ->getLatitudeUsing(fn($record) => $record->location['latitude'] ?? null)
    ->getLongitudeUsing(fn($record) => $record->location['longitude'] ?? null);

// Calculate distance between two points
$distance = $user->distanceTo(26.9124, 75.7873); // Returns distance in kilometers

// Calculate distance to another model
$distance = $user->distanceToModel($otherUser);

// Find users within 10km radius
$nearbyUsers = User::withinDistance(26.9124, 75.7873, 10)->get();

// Order users by distance from a point
$usersByDistance = User::orderByDistance(26.9124, 75.7873)->get();

// ❌ Wrong - Component doesn't have 'location' property
class OrdersStep extends Component
{
    public $data = []; // Missing 'location' key
}

// ✅ Correct - Component has the location property
class OrdersStep extends Component
{
    public $location = null; // Direct property
    // OR
    public $data = [
        'location' => null, // Nested property
    ];
}