Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use google maps api autocomplete.getPlace() without putting it in a place_changed listener?

I also tried to trigger the event

google.maps.event.trigger(autocomplete, 'place_changed');

but autocomplete.getPlace() is still return undefined until I change the city manually.

Any suggestions please?

like image 317
Zolyboy Avatar asked Feb 03 '26 02:02

Zolyboy


1 Answers

From google docs: getPlace(): Returns the details of the Place selected by user if the details were successfully retrieved. Otherwise returns a stub Place object, with the name property set to the current value of the input field.

Using for example the following code:

    var autocompleteSearch = new google.maps.places.Autocomplete(searchField, searchOptions);

    setInterval(function() {
        var place = autocompleteSearch.getPlace();
        console.log('place: ');
        console.log(place);
    }, 5000);

you will get 'undefined' in console if you wrote for example "qwertyasdfgh". Until you press Enter: after that the result will be:

place:  
Object {name: "qwertyasdfgh"} 

So, it's not much useful. No address_components, geometry...

like image 77
Anto Jurković Avatar answered Feb 05 '26 16:02

Anto Jurković