z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

google map is not working #5817

Open jesnagifto opened 1 year ago

jesnagifto commented 1 year ago

Description:

not get current location for google map

app/admin/bootstrap.php Encore\Admin\Form::extend('map', Encore\Admin\Form\Field\Map::class);

config/admin.php

'extensions' => [

    'latlong' => [

        // Whether to enable this extension, defaults to true
        'enable' => true,

        // Specify the default provider
        'default' => 'google',

        // According to the selected provider above, fill in the corresponding api_key
        'providers' => [

            'google' => [
                'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
            ],
    ]

]

controller.php

$form->map('latitude' , 'longitude', 'Map')->useGoogleMap();

Screenshot 2023-09-13 154359

very urgent ,please help me

Steps To Reproduce:

alexoleynik0 commented 1 year ago

Where from you get that latlong extension? GoogleMap is default map component, but you need to put your api_key in the .env file like this:

GOOGLE_API_KEY="xxxxxx"

Don't forget to php artisan config:clear if you've cached your configs. Should work even without ->useGoogleMap(); in the Form Controller. Also check that in config/admin.php there's 'map_provider' => 'google',

alexoleynik0 commented 1 year ago

You can also set default values like so:

$form->map('column_latitude', 'column_longitude', 'label')->default([
    'lat' => 51.378900135815,
    'lng' => 41.9005728960037,
]);

And check that values in the DB are correct: -90 and 90 for latitude and -180 to 180 for longitude

jesnagifto commented 1 year ago

thank you for your valuable time.. and the values are correctly saved the db.. but not show the current location

alexoleynik0 commented 1 year ago
  1. What happens when you zoom out the GoogleMap frame?
  2. What it shows for a new Instance in a Form (where there's no lat lng from the DB)?
  3. Is this issue also happens for "Map" view tab, or only on the "Satellite" tab?
jesnagifto commented 1 year ago

yes, the map can zoom out, then get the location.. but we want the automatically get the current location

alexoleynik0 commented 1 year ago

If you mean that current location should appear on the Creation form, than you probably should set it as defaults like I've shown above.

Google Maps does not detect current location by default, you should provide Lat Lng yourself.

There's a feature to set Device position. It requires browser's HTML5 Geolocation feature to be allowed for the site, you can try it if you want.

jesnagifto commented 1 year ago

we set the default value for the latitude and longitude, but not get the location

alexoleynik0 commented 1 year ago

Can you show the code of your form field with default lat lng, please?

jesnagifto commented 1 year ago

$form->map('latitude' , 'longitude', 'Map')->default(['lat' => 10.519110771042891, 'lng' => 76.23380817739174]);

jesnagifto commented 1 year ago

$form->latlong('latitude', 'longitude', 'Position')->default(['lat' => 10.520269394702767, 'lng' => 76.23250489204075])->zoom(20)->height(500); not get the location for this two codes ... please help me.

alexoleynik0 commented 1 year ago

Ok, so apparently there's an issue with Encore\Admin\Form\Field->value method.

Because it checks only for $this->value to not be equal to null (using null coalescing operator -- return $this->value ?? $this->getDefault();). Hence it does not get to the getDefault method if $this->value is something else from null, like an array with empty values as in our case -- from Encore\Admin\Form\Field\Map

protected $value = [
    'lat' => null,
    'lng' => null,
];

I'm going to open an issue on this strange behavior of the Field / Map class.

In the meantime you have a couple of options to deal with it:

  1. Create our own Map field with the bug fixed. Something like:
    
    <?php

namespace App\Admin\Extensions\Form;

use Encore\Admin\Form\Field\Map;

class MapCustom extends Map {

/**
 * View for field to render.
 *
 * @var string
 */
protected $view = 'admin::form.map';

/**
 * Set or get value of the field.
 *
 * @param null $value
 *
 * @return mixed
 */
public function value($value = null)
{
    if ($value === null) {
        if (is_array($this->value)) { // this is added, change it if you want
            foreach ($this->value as $val) {
                if (null === $val) {
                    return $this->getDefault();
                }
            }
        }
        return $this->value ?? $this->getDefault();
    }

    $this->value = $value;

    return $this;
}

}

Where you can also edit something about how JS is set for `initGoogleMap` function. 

2. Use Field's `with` method in your Controller, that allows to change value of the Field right before render:
```php
$form->map('lat', 'lng', __('MAP'))
    ->with(function ($value) {
        if (null !== $value['lat']) {
            return $value;
        }

        return [
            'lat' => 10.520269394702767,
            'lng' => 76.23250489204075,
        ];
    })
;

This should work both for Creating and Editing. The only note here is that it won't show that this Entity does not have lat lng set yet in the form (for Editing), but you can add some helper text for that.

Hope that helps.