Umar Bolatov
Umar Bolatov

Reputation: 31

Restrict Google Maps API v3 search results to a country while biasing them to a location

I'm trying to restrict search results to a country and heavily bias them to a particular lat/lng (but not restrict).

I'm using the "bounds" field to bias the search results returned by the API to user's location (documentation). This works well, however, I also need to restrict the returned results to a country.

I'm using the "region" field for that but it does not restrict the results to a country and only biases them. I tried using the "componentRestrictions" field to restrict results to a country. This works well, however, the location biasing I set-up initially (with the "bounds" field) seems to stop working (or at least working differently now).

Any ideas on how one might achieve my goal here?

Example

I'm sending a geocoder request for "35 bastion st." with region set to CA (Canada) with bounds set-up to be 2.5 degrees around Vancouver, BC, Canada. There are two search results that can come up: one in Toronto, Ontario, Canada and another in Nanaimo, British Columbia, Canada. I expect the Nanaimo one to come up in this case because it's the closer location and it does. Link: https://maps.googleapis.com/maps/api/geocode/json?address=35%20bastion%20st.&region=CA&bounds=46.7827,-125.6207|51.7827,-120.620&key=AIzaSyD3y1Mk-vZjfgqg1GB_dOWMDo004oY8xus

Now if I have the same request but with the country filter applied I would expect the same result to be returned but that is not what happens. Link: https://maps.googleapis.com/maps/api/geocode/json?address=35%20bastion%20st.&region=CA&bounds=46.7827,-125.6207|51.7827,-120,%20-122.1207&components=country:CA&key=AIzaSyD3y1Mk-vZjfgqg1GB_dOWMDo004oY8xus

Edit:

Corrected second link: https://maps.googleapis.com/maps/api/geocode/json?address=35%20bastion%20st.&bounds=46.7827,-125.6207|51.7827,-120.620&components=country:CA&key=AIzaSyD3y1Mk-vZjfgqg1GB_dOWMDo004oY8xus

Upvotes: 1

Views: 372

Answers (1)

Umar Bolatov
Umar Bolatov

Reputation: 31

According to Google this is not a bug (see here: https://issuetracker.google.com/issues/123557217).

According to our engineers, this is working as intended.

The reason being that the country component will take priority when using the two parameters, as the use-cases of the two are entirely different.

Moreover, "35 Bastion st" is very ambiguous and it should be searched using Autocomplete where viewports/bounds can influence the results better.

Upvotes: 1

Related Questions