Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map Field Having Issue when Added in Custom Field #145

Open
rbiscocho opened this issue Feb 20, 2025 · 0 comments
Open

Map Field Having Issue when Added in Custom Field #145

rbiscocho opened this issue Feb 20, 2025 · 0 comments

Comments

@rbiscocho
Copy link

rbiscocho commented Feb 20, 2025

Map Field is having issue when added in Custom Field,

here is the sample field.

`<?php

declare(strict_types=1);

namespace App\FilamentTenant\Support\Forms;

use Cheesegrits\FilamentGoogleMaps\Fields\Map;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Get;
use Filament\Forms\Set;

class LocationPickerField extends Field
{
protected string $view = 'filament-forms::components.group';

protected function setUp(): void
{
    parent::setUp();

    $this->schema([
        Group::make([
            Group::make([
                TextInput::make('full_address')
                    ->label(trans('Search Address'))
                    ->dehydrated(false)
                    ->columnspan(2)
                    ->placeholder('Search Address'),
                TextInput::make('address_name')
                    ->label(trans('Address Name'))
                    ->columnspan(2)
                    ->placeholder('Add Address Name'),
                TextInput::make('latitude')
                    ->lazy()
                    // ->default('14.5454321')
                    ->afterStateUpdated(function ($state, Get $get, Set $set) {
                        $set('map', [
                            'lat' => floatval($state),
                            'lng' => floatval($get('longitude')),
                        ]);
                    })
                    ->columnspan(1),
                TextInput::make('longitude')
                    ->lazy()
                    // ->default('121.0686773')
                    ->afterStateUpdated(function ($state, Get $get, Set $set) {
                        $set('map', [
                            'lat' => floatval($get('latitude')),
                            'lng' => floatval($state),
                        ]);
                    })
                    ->columnspan(1),
            ])->columns(2),
            Map::make('map')
                ->dehydrated(false)
                ->mapControls([
                    'mapTypeControl' => true,
                    'scaleControl' => true,
                    'streetViewControl' => true,
                    'rotateControl' => true,
                    'fullscreenControl' => true,
                    'searchBoxControl' => false, // creates geocomplete field inside map
                    'zoomControl' => false,
                ])
                ->height(fn () => '400px') // map height (width is controlled by Filament options)
                ->defaultZoom(18) // default zoom level when opening form
                ->autocomplete('full_address') // field on form to use as Places geocompletion field
                ->autocompleteReverse(true) // reverse geocode marker location to autocomplete field
                ->draggable() // allow dragging to move marker
                ->defaultLocation([
                    '14.5454321',
                    '121.0686773'
                ]) 
                ->afterStateUpdated(function ($state, Get $get, Set $set) {
                   
                    $set('latitude', $state['lat']);
                    $set('longitude', $state['lng']);
                })
                ->afterStateHydrated(function (Map $component, Get $get, Set $set) {

                    $lat = $get('latitude');
                    
                    if(!is_null($lat)) {
                        $component->state([
                            'lat' => floatval($get('latitude')),
                            'lng' => floatval($get('longitude')),
                        ]);
                    }
                }),
           
        ])
            ->columns(2),
      
    ]);
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant