Google API自动完成,只有一种区域类型



我当前正在使用Google API API自动完成位置。我可以将其设置为仅返回使用" autocomplete.setTypes(['grounions'](的区域类型"。查看文档,我可以看到区域类型将返回包含以下类型的所有结果;
*局部
* sublocality
* postal_code
*国家
* Administrative_area1
* Administrative_area2

我想实现的目标是仅返回带有" sublocality"区域类型的自动完成预测。这可能吗?如果没有人也有类似的问题,他们会推荐其他解决方案?谢谢。

var autocomplete = new google.maps.places.Autocomplete(input);
var types = ['(regions)'];
var options = {
    componentRestrictions: {country: 'uk'}
};
autocomplete.setTypes(types);
autocomplete.setOptions(options);

you 可以 仅显示查询预测结果(或对此做任何您喜欢的事情,因为我们不知道您想做什么(一个预测是否具有sublocality的类型,因为每个结果都带有types的数组。

类似的东西:

// 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
  }
}

裸露的记录,尽管查询预测仅返回最多5个结果,因此它可能会使最终用户感到困惑,因为直到您输入相当多的字母。。

最新更新