mostafaznv / nova-map-field

Spatial Map Fields for Laravel Nova
MIT License
42 stars 15 forks source link

Issue: Using defaultLatitude/Longtitude when a record is (null) doesn't show marker #26

Closed BerkanKaya closed 1 year ago

BerkanKaya commented 1 year ago

Hello. I am trying to use defaultLatitude/Longtitude for records that have a value of (null) in my 'geolocation' column like this: MapPointField::make('Location', 'geolocation')->defaultLatitude((51.5887845))->defaultLongitude((4.7760237))->hideFromIndex(),

But when i do this, the marker doesn't show itself like this: image

I want it to show the marker like when 'geolocation' does have a value: image

I also tried this: MapPointField::make('test')->defaultLatitude((34.10362671361177))->defaultLongitude((-118.3663426622582))->hideFromIndex(), but this doesnt work either.

Am i doing something wrong, Or is this not possible with this package?

mostafaznv commented 1 year ago

Hi @BerkanKaya

defaultLatitude and defaultLongitude are methods to set default location of maps (not fields). To set default value for fields, please use default methods. I added this feature in the latest version of the package.

Please update to latest version. v1.8.0

Example:

class Location extends Resource
{
    public function fields(Request $request): array
    {
        return [
            ID::make()->sortable(),

            MapPointField::make(trans('Location'), 'location')
                ->defaultLatitude(35.6978527)
                ->defaultLongitude(51.4037269)
                ->default(
                    PointValue::make(35.6978527, 51.4037269)
                )
        ];
    }
}
BerkanKaya commented 1 year ago

Hi @mostafaznv

Even after updating the package, and using this: MapPointField::make(trans('geolocation'), 'geolocation') ->defaultLatitude(35.6978527) ->defaultLongitude(51.4037269) ->default( PointValue::make(35.6978527, 51.4037269) )

I am still not able to see the marker: image

i want the marker to be there like this: image The marker is only shown when the 'geolocation' column in my table has a value. But i want the marker to show even if the value is (null) by using this: MapPointField::make(trans('geolocation'), 'geolocation') ->defaultLatitude(35.6978527) ->defaultLongitude(51.4037269) ->default( PointValue::make(35.6978527, 51.4037269) )

Is something like this possible with your package? Thanks in advance 😄 !

mostafaznv commented 1 year ago

The default value only works on create form. If you have an existing record and it has a Point/Location column with null value, you can handle it with Laravel Accessors

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use MatanYadaev\EloquentSpatial\Objects\Point;
use Mostafaznv\NovaMapField\Traits\HasSpatialColumns;

class Location extends Model
{
    use HasSpatialColumns;

    protected $fillable = [
        'location'
    ];

    protected $casts = [
        'location' => Point::class
    ];

    protected function location(): Attribute
    {
        return Attribute::make(
            get: function($value) {
                if (is_null($value)) {
                    return new Point(51.5032973, -0.1217424); // default value
                }

                return Point::fromWkb($value);
            },
        );
    }
}