google-mapsgoogle-maps-api-3google-places-apigoogle-places-autocomplete

How to restrict autocomplete suggestions to given state?


I have an address form and I'm using Google's Places Autocomplete (new) API to autocomplete the fields.

I have 3 fields:

If the user selects Alabama, I want to show localities for the state of Alabama only.

screenshot

I know that:

  1. I can restrict results to countries with regionCode.
  2. I can use 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_addresses 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.


Solution

  • 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.