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?
This involves just adding a fragment to your activity's XML layout and a listener to your activity or fragment. Next, initialize the AutocompleteSupportFragment and set the fields to specify which types of place data to return in the activity or fragment. If playback doesn't begin shortly, try restarting your device.
Use Cloud-based Maps Styling To get started with Cloud-based maps styling, copy the JSON style above, then go to the Google Cloud console. To create a new map style, paste the JSON into the 'Import JSON' option. Cloud-based maps styling is available for the Maps JavaScript API at no extra charge.
Currently, you can use componentRestrictions to filter by country. The country must be passed as as a two character, ISO 3166-1 Alpha-2 compatible country code. Save this answer.
How do I limit Google autocomplete results to city only? List only cities in the country var options = { types: ['(cities)'], componentRestrictions: {country: “us”} }; List all cities, states and regions in the country var options = { types: ['(regions)'], componentRestrictions: {country: “us”} };
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With