I have an address form and I'm using Google's Places Autocomplete (new) API to autocomplete the fields.
I have 3 fields:
country
administrative_area_level_1
(State / Province)locality
(City / Town)If the user selects Alabama, I want to show localities for the state of Alabama only.
I know that:
regionCode
.locationBias
.regionCode
works great. If the user selects a country, I use it to filter the other fields. Unfortunately, it only works with countries.
locationBias
has a max radius of 50,000 meters. This is not enough to cover an entire state. Also, it doesn't restrict anything, so results are returned from all over the world.
I tried to append the state name to the search input input += " Alabama"
, but this way the autocomplete isn't returning anything. *
NOTE: Alabama is just an example. I would like autocomplete to work with any state in any country. I would also like autocomplete to work for street_address
es inside a locality this way.
Is there a way to achieve what I want?
* EDIT: here's a minimal reproducible example of why appending the state to the input is not working for me, as requested by @MrUpsidown. I'm trying to search for Huntsville, Alabama by inputting "Hunt" in the City / Town field, the call yields 0 results.
AutocompleteSuggestion.fetchAutocompleteSuggestions(
{
includedPrimaryTypes: ["locality", "postal_town"],
input: "Hunt" + " Alabama",
});
Although the libraries I use to access the API shouldn't make a difference, I'll mention I'm using the Maps JavaScript API and the visgl/react-google-maps libraries for completeness sake.
It seems like it's not possible. I've submitted a feature request: https://issuetracker.google.com/issues/373290671.
It's unfortunate and maybe there's different API that solves this problem. I went with fetching the coordinates from Places Details (New)
and using locationBias
with the max radius of 50,000 meters. It's not perfect but it's better than nothing.