diogogpinto/filament-geolocate-me

Filament Geolocate Me

v0.1.1 2025-02-14 00:46 UTC

This package is auto-updated.

Last update: 2025-03-14 00:57:40 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Want to get a user's location before an Action is performed on Filament?

This Filament plugin adds geolocation capabilities to your Filament Actions, allowing you to easily capture and use a user's geographic location. Also, it's plug and play! 🚀

  • 🎯 Gets real-time user location with browser's Geolocation API
  • âš¡ Halts action execution until location is captured
  • 🔄 Automatically disables other actions while waiting
  • 🛠 Injects location data into before(), action(), and after() methods
  • 🔒 Built-in validation and error handling
  • 📦 Zero configuration required - works out of the box
  • 🎨 Filament-Native Experience

Navigation

Installation

You can install the package via composer:

composer require diogogpinto/filament-geolocate-me

Usage

Basic Usage

Import the HasGeolocation trait in the Livewire component where your Filament Action is and add geolocation to any Filament action using the withGeolocation() method:

use Filament\Actions\Action;
use DiogoGPinto\GeolocateMe\Data\Coordinates;

//Import the HasGeolocation trait
use HasGeolocation;

Action::make('checkIn')
    ->withGeolocation()
    ->action(function (Coordinates $coordinates) {
        // Handle the location data in your action
        $latitude = $coordinates->latitude;
        $longitude = $coordinates->longitude;
        $accuracy = $coordinates->accuracy; // in meters, optional
    });
    ->before(function (Coordinates $coordinates) {
        // Do something with location data before running the action
    });
    ->after(function (Coordinates $coordinates) {
        // Do something with location data after running the action
    });

Caution

This plugin only works with custom Filament Actions. It is NOT compatible with Filament's prebuilt actions (like EditAction, CreateAction, etc.)

Tip

Location data (Coordinates) can be accessed in three action phases: before, action and after.

Error Handling

The plugin automatically handles geolocation errors. You can check for errors in your action:

Action::make('checkIn')
    ->withGeolocation()
    ->action(function (Coordinates $coordinates) {
        if ($coordinates->hasError()) {
            // Handle error case
            $errorMessage = $coordinates->error;
            return;
        }
        
        // Process location data
    });

Action Lifecycle

The geolocation process follows this sequence:

  1. Action is triggered and immediately halted
  2. All other actions are temporarily disabled until the location process finishes
  3. Browser requests location permission
  4. Filament waits for location from Alpine
  5. Loading state is shown
  6. Location is captured and the action is fired
  7. Action is executed with location data injected

Coordinates Data Structure

The Coordinates class provides the following properties:

$coordinates->latitude;   // float: -90 to 90
$coordinates->longitude;  // float: -180 to 180
$coordinates->accuracy;   // float|null: accuracy in meters
$coordinates->error;      // string|null: error message if something went wrong

Validation

The plugin automatically validates coordinates to ensure they are within valid ranges:

  • Latitude: -90 to 90 degrees
  • Longitude: -180 to 180 degrees

Custom Styling

The plugin uses Filament's default loading indicators. You can customize the appearance through Filament's theming system.

Examples

Basic Check-in System

use DiogoGPinto\GeolocateMe\Data\Coordinates;

Action::make('checkIn')
    ->withGeolocation()
    ->action(function (Coordinates $coordinates) {
        //Handle Error - if you can't locate the user, do nothing
        if ($location->hasError()) {
            Notification::make()
                ->danger()
                ->title('Error retrieving location')
                ->body($location->error)
                ->send();

            return;
        }

        //Create a checkin
        CheckIn::create([
            'latitude' => $coordinates->latitude,
            'longitude' => $coordinates->longitude,
            'accuracy' => $coordinates->accuracy,
            'user_id' => auth()->id(),
        ]);
        
        Notification::make()
            ->success()
            ->title('Checked in successfully')
            ->send();
    });

Requirements

  • PHP 8.1 or higher
  • Laravel 10.0 or higher
  • FilamentPHP 3.0 or higher
  • Browser with Geolocation API support

Browser Support

This plugin relies on the Geolocation API, which is supported by all modern browsers. Users will need to grant location permissions for the feature to work.

Security

The plugin handles location data on the client side only when explicitly requested through an action. All coordinate validation is performed server-side to ensure data integrity.

Production Usage Warning

Caution

This plugin is currently in its early stages (v0.1) and should be used with caution in production environments. While it has been tested in basic scenarios, it may contain bugs or unexpected behaviors. We recommend:

  1. Thoroughly testing the plugin in your specific use case
  2. Having proper error handling in place
  3. Testing across different browsers and devices
  4. Having a fallback mechanism in case of geolocation failures
  5. Monitoring for any issues in production
  6. Keeping the plugin updated for security patches and improvements

Please report any issues or suggestions on our GitHub Issues page.

Credits

License

The MIT License (MIT). Please see License File for more information.