Akhil
Akhil

Reputation: 99

Restricting response in the new Google Places API (New)

I am using the "Place autocomplete" feature of the new Google Places API. My requirement is that I want to restrict my response to a specific country. In the old Google Places API It was possible to use it like this

https://maps.googleapis.com/maps/api/place/autocomplete/json
?input=Karn
&key=<key>
&type=(regions)
&components=country:in

But when it comes to the new version, I couldn't find it anywhere in the document on how to restrict responses to a specific country. Also, I couldn't find anywhere on how I can pass multiple values to the key "includedPrimaryTypes"

https://places.googleapis.com/v1/places:autocomplete
?key =<key>
&fields=suggestions.placePrediction.structuredFormat.mainText.text
&input=Karn
&includedPrimaryTypes=(regions)

Upvotes: 0

Views: 107

Answers (1)

Hogan
Hogan

Reputation: 11

I believe in your case you just need to replace "components" with "componentRestrictions" in your URL.

I'm using the JS object and the format for me is:

const placeAutocomplete = new google.maps.places.PlaceAutocompleteElement({
    componentRestrictions: {country: ['gb', 'es']},
    requestedLanguage: 'en-GB',
    requestedRegion: 'uk',
});

Where

  • "componentRestrictions::country" is an array of country codes to limit results from
  • "requestedLanguage" is the language to return results in (where possible)
  • "requestedRegion" specifies result formatting to the relevant region

Upvotes: 0

Related Questions