How do I restrict a Google Geocode query to multip

2019-07-09 01:21发布

问题:

I am trying to send a user query to Google Maps Geocoding API, and restrict it to the countries that my service will be operating in. If I just want results from the UK, then the following query works when searching for quay:

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=$apiKey&input=quay&language=en-gb&location=0,0&radius=50000&components=country:uk

If I want to restrict the results to just France then the following works:

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=$apiKey&input=quay&language=en-gb&location=0,0&radius=50000&components=country:uk

According to https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering A filter consists of a list of component:value pairs separated by a pipe (|). So in order to search both the UK and France, I thought the following would work:

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=$apiKey&input=quay&language=en-gb&location=0,0&radius=50000&components=country:uk|fr

But it doesn't, instead it returns the following:

{ "predictions": [], "status": "INVALID_REQUEST" }

I've also tried changing the components piece at the end of the URL to components=country:uk|country:fr. This appears to work, but all it is doing is searching whatever is the last country:value on the URL.

As far as I can tell, my options are:

  • Search the UK and France separately, and then combine the results. This will require twice as many calls and will go up with each country added. I will also need to determine how to weight the results.
  • Remove the component restriction so that I get all results from around the world, and filter the answers myself to the UK and France. There's a good chance that the results that the Geocoding API returns will result in no results to display.

How can I make one call to return results just restricted to the UK and France? Is this even possible?

回答1:

This is currently not possible in 1 call, see https://code.google.com/p/gmaps-api-issues/issues/detail?id=4233