dotswan / filament-map-picker

Map Picker is a Filament custom field designed to simplify the process of choosing a location on a map and obtaining its geo-coordinates.
MIT License
56 stars 13 forks source link

The marker is always on the center of the map #18

Closed steamn closed 4 months ago

steamn commented 5 months ago

Whenever I try to change any entry, the map pointer is always in the same position. I can move it and it will correctly display and save the coordinates to the database, but when the page is refreshed, the marker is again in the center of the map (in the same position).

I think this is because $record from afterStateHydratedreturns null, but I'm new to filament and don't understand how to properly pass $record->latitude value.

Any help would be appreciated.

mohaphez commented 5 months ago

Hi @steamn

I would be grateful if you could share the code written with your database structure.

steamn commented 5 months ago

@mohaphez

Here is the migration:

Schema::create('buildings', function (Blueprint $table) {
            $table->id();
            $table->string('latitude');
            $table->string('longitude');
            $table->string('location')->nullable();
            other_fields;
 });

BuildingResource.php

return $form
            ->schema([
                TextInput::make('latitude'),
                TextInput::make('longitude'),

                Map::make('location')
                    ->label('location')
                    ->columnSpanFull()
                    ->afterStateUpdated(function (Get $get, Set $set, string|array|null $old, ?array $state): void {
                        $set('latitude', $state['lat']);
                        $set('longitude', $state['lng']);
                    })
                    ->afterStateHydrated(function ($state, $record, Set $set): void {
                        $set('location', ['latitude' => $record?->latitude, 'longitude' => $record?->longitude]);
                    })
                    ->extraStyles([
                        'min-height: 400px',
                    ])
                    ->liveLocation()
                    ->minZoom(2)
                    ->showMarker()
                    ->markerColor("#22c55eff")
                    ->showFullscreenControl()
                    ->showZoomControl()
                    ->draggable()
                    ->detectRetina()
                    ->showMyLocationButton()
                    ->extraTileControl([])
                    ->extraControl([
                        'zoomDelta'           => 1,
                        'zoomSnap'            => 1,
                    ]),

                Actions::make([
                    Action::make('default_location')->label('Default location')
                        ->icon('heroicon-m-map-pin')
                        ->action(function (Set $set, $state, $livewire): void {
                            $set('location', ['lat' => '43.207678268758', 'lng' => '23.556060791015625']);
                            $set('latitude', '43.207678268758');
                            $set('longitude', '23.556060791015625');
                            $livewire->dispatch('refreshMap');
                        })
                ])->verticalAlignment(VerticalAlignment::Start),

               ->other_fields;
 ])
            ->model(Building::class);
    }

Edit. The coordinates are saved correctly, but after refreshing the page the map looks like this. The marker is placed not at the coordinates. https://imgur.com/bVhy7Od

mohaphez commented 5 months ago

You don't need the location column in database

If you want to save latitude and longitude in separate columns, perhaps this issue could be helpful: #2.

If not, please let me know.

steamn commented 5 months ago

If I remove the location column from the database I get SQLSTATE[HY000]: General error: 1 no such column: location error.

Sorry, I still don't get how it should work.

Is there any filament repository with this plugin to inspect the code?

mohaphez commented 5 months ago

Have you removed the location field from the $fillable variable in your model?

steamn commented 5 months ago

I've Model::unguard(); in AppServiceProvider boot method so my models fillables are empty.

mohaphez commented 5 months ago

You need to define $fillable or use the second method and cast the location.