简体   繁体   中英

How can I programmatically change the selection of a google maps api autocomplete input?

I understand that in order to fire the place_changed event programmatically with the google maps javascript API v3 you do the following:

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

However this simply fires the callback specified in the event and does not actually do anything to the <input> element that is attached.

What I need to do is programmatically change the selection in the autocomplete <input> to a specific location or place specified within the place object retrieved earlier via:

autocomplete.getPlace()

I can of course directly change the value in the input:

input.value = 'Whatever';

But doing this does not change the autocompletes selection. After doing so the user has to delete the whole string in the <input> , and then finally begin typing again, in order to get the autocomplete predictions to show up again.

So to summarise I would like to change the selection of a google maps autocomplete input programmatically, using a place object obtained from the places/autocomplete api. Is there any way to do this?

If you have the place object you can do it via

autocomplete.set("place", place)

That will trigger the place_changed event in the autocomplete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM