Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 325
Gordo Avatar asked Jul 25 '13 17:07

Gordo


People also ask

How would you implement Google places autocomplete programmatically?

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.

How do I customize Google Maps API?

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.

How do I limit autocomplete results to country only?

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 and country only Android?

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”} };


1 Answers

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

like image 189
jeffsaracco Avatar answered Nov 06 '22 05:11

jeffsaracco