I actually implemented Google Maps autocomplete on my Inertia + VueJS app.
In my case, the user can add multiple autocomplete fields (as they can add multiple stops on a trip).
This is what the code that sets up the autocomplete looks like:
let ac_input = document.getElementById(`google_ac_${this.stopIndex}`);
const ac = new this.google.places.Autocomplete(ac_input, {
componentRestricitons: { country: 'us' },
fields: ['address_component', 'name', 'formatted_address'],
});
ac.addListener('place_changed', () => {
let place = ac.getPlace();
this.stop.location_name = place.name;
for (var i = 0; i < place.address_components.length; i++) {
let addressType = place.address_components[i].types[0];
if (this.componentForm[addressType]) {
let val = place.address_components[i][this.componentForm[addressType]];
this.stop[this.addressTypeConversions[addressType]] = val;
}
}
});
Let it note that in my template, I'm defining every input that will hold the autocomplete as "google_ac_${index}", so that I'm able to catch it within the code.
Also, for the code above to work, I created this two properties in my Vue Component:
componentForm: {
name: 'name',
street_number: 'short_name',
route: 'short_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'short_name',
postal_code: 'short_name',
},
addressTypeConversions: {
name: 'name',
street_number: 'exterior_number',
route: 'street_name',
locality: 'city_label',
administrative_area_level_1: 'state_label',
postal_code: 'zip_code',
},