Open jesnagifto opened 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',
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
thank you for your valuable time.. and the values are correctly saved the db.. but not show the current location
lat lng
from the DB)?yes, the map can zoom out, then get the location.. but we want the automatically get the current location
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.
we set the default value for the latitude and longitude, but not get the location
Can you show the code of your form field with default lat lng, please?
$form->map('latitude' , 'longitude', 'Map')->default(['lat' => 10.519110771042891, 'lng' => 76.23380817739174]);
$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.
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:
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.
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' => [
]
controller.php
$form->map('latitude' , 'longitude', 'Map')->useGoogleMap();
very urgent ,please help me
Steps To Reproduce: