laravel-admin icon indicating copy to clipboard operation
laravel-admin copied to clipboard

google map is not working

Open jesnagifto opened this issue 1 year ago • 11 comments

  • Laravel Version: "^8.83",
  • PHP Version:"^8.0.2|^8.2.4",
  • Laravel-admin: "^1.8"

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:

jesnagifto avatar Sep 13 '23 10:09 jesnagifto

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 avatar Sep 13 '23 13:09 alexoleynik0

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

alexoleynik0 avatar Sep 13 '23 13:09 alexoleynik0

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

jesnagifto avatar Sep 15 '23 04:09 jesnagifto

  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?

alexoleynik0 avatar Sep 15 '23 14:09 alexoleynik0

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

jesnagifto avatar Sep 26 '23 07:09 jesnagifto

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.

alexoleynik0 avatar Sep 26 '23 13:09 alexoleynik0

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

jesnagifto avatar Sep 28 '23 09:09 jesnagifto

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

alexoleynik0 avatar Sep 28 '23 09:09 alexoleynik0

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

jesnagifto avatar Sep 28 '23 09:09 jesnagifto

$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.

jesnagifto avatar Sep 28 '23 10:09 jesnagifto

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.

  1. Use Field's with method in your Controller, that allows to change value of the Field right before render:
$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.

alexoleynik0 avatar Sep 29 '23 14:09 alexoleynik0