filament-locationpickr-field

Prerequisites

This package is built for Laravel and Filament.

Installation

1composer require lara-zeus/filament-locationpickr-field

Publish the config file:

1php artisan vendor:publish --tag=filament-locationpickr-field-config

Optionally publish views:

1php artisan vendor:publish --tag=filament-locationpickr-field-views

Setting your Google Maps API Key

All use of the Google Maps API requires an API key. If you don't have one, refer to Google's documentation.

Once you have a key, either add it to your .env file as:

1GMAP_API=your_map_api_key_here

...

or publish and edit the filament-locationpickr-field.php config file.

Preparing the models

Add a location column to any model migration schema used for map data

1...
2 
3Schema::table('table_name', function (Blueprint $table) {
4 $table->json('location')->nullable();
5});
6 
7...

and add the column to the model fillable array

1...
2 
3protected $fillable = [
4 ...
5 'location',
6];

If you have separate columns for lat and lng you can use a computed property on any model being used for map data and use a mutator which converts between separate lat and lng fields on your table, and a Google Point style array of 'lat' and 'lng' keys.

1...
2 
3protected $appends = [
4 ...
5 'location',
6];
7 
8...
9 
10public function location(): Attribute
11{
12 return Attribute::make(
13 get: fn ($value, $attributes) => json_encode([
14 'lat' => (float) $attributes['lat'],
15 'lng' => (float) $attributes['lng'],
16 ]),
17 set: fn ($value) => [
18 'lat' => $value['lat'],
19 'lng' => $value['lng'],
20 ],
21 );
22}
23 
24...

Our Sponsors

Zeus is an open-source project. Thanks to our sponsors for helping maintain these packages.