0

I am currently using the google apis API to autocomplete locations. I can set it to only return the regions type with "autocomplete.setTypes(['regions'])" . Looking at the documentation I can see that regions type will return all results containing the following types;
* locality
* sublocality
* postal_code
* country
* administrative_area1
* administrative_area2

What I want to achieve though, is to only return autocomplete predictions with the region type "sublocality". Is this possible? If not has anybody had a similar issue and have an alternative solution they would recommend? Thank you.

var autocomplete = new google.maps.places.Autocomplete(input);

var types = ['(regions)'];

var options = {
    componentRestrictions: {country: 'uk'}
};

autocomplete.setTypes(types);
autocomplete.setOptions(options);

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • What about reading the [documentation](https://developers.google.com/places/supported_types#table3)? `geocode`, `address`, `establishment`, `(regions)`, `(cities)`, are the supported types. – MrUpsidown Feb 08 '18 at 09:06
  • 2
    I did read the documentation. That is why I wrote "Looking at the 'documentation' I can see that regions type will return all results containing the following types; * locality * sublocality * postal_code * country * administrative_area1 * administrative_area2 for region type " – Connor Lloyd Moore Feb 08 '18 at 11:12
  • @MrUpsidown clearly it didn't and your own use of the word 'should', should, have been a self monitoring tool of how to respond, or indeed whether to. Let's make ourselves more accessible to the community and at least be mindful of different perspectives and levels of experience. Telling someone to RTFM is not helpful. The question clearly contains the following "Looking at the documentation I can see that regions type will return all results containing the following types". – Lloyd Moore Feb 08 '18 at 21:17

1 Answers1

0

You could filter your query predictions results to only display (or do whatever you like with it, as we don't know what you are trying to do) a prediction if it has a type of sublocality as each result comes with an array of types.

Something like that:

// Build output for each prediction
for (var i = 0, prediction; prediction = predictions[i]; i++) {

  if (prediction['types'].includes('sublocality')) {

    // Display it or do what you need to do here
  }
}

Bare in mind though that query predictions only return a maximum of 5 results, so it might be confusing for the end-user as nothing at all might show until you enter quite a few letters...

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131