egeloen / IvoryGoogleMapBundle

Google Map API v3 integration for your Symfony2 project.
https://github.com/egeloen/ivory-google-map
MIT License
217 stars 152 forks source link

Get place details #196

Closed ScherlOMatic closed 7 years ago

ScherlOMatic commented 7 years ago

Hello!

First of all thanks for this great plugin but unfortunately i ran into a problem.

Is there a way to get the place details within my symfony controller? I saw that there is a request with all the information i need. The request looks like this and is automatically fired after selecting one item from the suggestions. https://maps.googleapis.com/maps/api/place/js/PlaceService.GetPlaceDetails?.....

br Stefan

egeloen commented 7 years ago

Hey @ScherlOMatic !

Regading the autocomplete form type, natively, it has been designed to only give you a rich input in your browser (not to get place details automatically in your controller). It is possible but will require extra work.

To do it, I would hook in the javascript and populate extra form fields automatically (such as zip code, city, whatever, ...) and then these informations will be forwared to the controller via the form.

To hook in the javascript, you can use event and attach it to the place_changed event.

ScherlOMatic commented 7 years ago

Hey!

Thanks for your help but I'm still wondering how to use the events. This is my code so far.

$builder->add('location', PlaceAutocompleteType::class, array(
    'components' => array(AutocompleteComponentType::COUNTRY => $options['country']),
    'types' => array(AutocompleteType::GEOCODE),
    'label' => 'Where do you live.',
    'attr' => array('placeholder' => ''),
    'events' => [$event],
));

And I found something about the $event object which looks like this, but what should the first parameter be? $event = new Event( $this, 'click', 'function(){alert("Marker clicked!");}' );

another question regarding the placeholder. is there an option (eg. false, null) which disables and removes the attribute from the tag, instead of passing an empty value? and the label tag renders with an "active" class within my project, is that a bug or a feature ;-) it's correct when putting the focus on the input tag, but it is always there.

thx again

egeloen commented 7 years ago

What I would use (live coded, not tested):

// Javascript variable used for your autocomplete
$variable = 'place_autocomplete';

// Javascript closure triggered when the user will select a place
$handle = <<<EOF
function () {
    var place = $variable.getPlace();
    // Update your form according to the place selected by the user
}
EOF;

// Create the event
$event = new Event($variable, 'place_changed', $handle);

You can maybe remove the $variable and rely on this in the javascript closure (need testing...) and then:

$builder->add('location', PlaceAutocompleteType::class, [
    'components' => [AutocompleteComponentType::COUNTRY => $options['country']],
    'types' => [AutocompleteType::GEOCODE],
    'label' => 'Where do you live.',
    'attr' => ['placeholder' => ''],
    'variable' => $variable,
    'events' => [$event],
]);
egeloen commented 7 years ago

For your placeholder question, I don't really understand. Can you clarify? I double check the PHP/Twig template and the way the place autocomplete html input is rendered and I don't see default value for the placeholder attr. So, if there is a placeholder by default it comes from Google and IMO, the library should not alter this default behavior. Since in Symfony the way to override the the placeholder is what you already do, I don't want to add an other option to configure such thing.

For the "active" class, I also double check in the code and there is nothing related so it should come from Google or something in your code alters it.

egeloen commented 7 years ago

@ScherlOMatic Any news?

ScherlOMatic commented 7 years ago

Hey! I did give a shot but somehow the event didn't fire up the dom. It is included in the javascript and seems to compile correctly. If I pass false data I get a javascript error. Atm I'm on vacation but will work on it as soon as i get back. (i need it for our website which should go online this march)

will let you know asap

ScherlOMatic commented 7 years ago

Hello!

I did some more research on the placeholder issue and it really comes from google itself. https://developers.google.com/maps/documentation/javascript/places-autocomplete#placeholder_text I also found a solution to this problem on SO http://stackoverflow.com/questions/41621886/remove-placeholder-text-from-google-maps-places-api

thx

egeloen commented 7 years ago

Ok, so can we close the issue since there is nothing to fix in the bundle?

ScherlOMatic commented 7 years ago

Hello!

No not yet cause our issue with the event handling is still open.

I created a codepen document where you can also do some live testing http://codepen.io/anon/pen/jBMBMQ

I used the event code from your suggestion in post #4 but the event never fires.

Do you have any hints for me? And how can i put data into this? "events": { "dom_events": [], "dom_events_once": [], "events": [], "events_once": [] }

thx

egeloen commented 7 years ago

I have give a try to your issue and everything is working (the event is triggered and I can get the select place).

Take a look to https://github.com/symfony/symfony-standard/compare/master...egeloen:google-map-bundle-issue-196 (The api key has been revoked) :)

Then, you can interact with the place using https://developers.google.com/maps/documentation/javascript/reference#PlaceResult

egeloen commented 7 years ago

@ScherlOMatic Does my explanation as well as code example help you?

ScherlOMatic commented 7 years ago

Yes it works now.

The thing which got me distracted was the "variable" parameter. I still don't know what it's for and where the values ("autocomplete") come from.

thx again for your help

egeloen commented 7 years ago

The variable is the variable used when instantiating the autocompete in javascript:

var autocomplete = new google.maps.place.Autocomplete();

Closing as everything is fixed now :)