首页 文章

Google API自动填充功能,只有一种地区类型

提问于
浏览
0

我目前正在使用google apis API自动填充位置 . 我可以将其设置为仅返回"autocomplete.setTypes(['regions'])"的区域类型 . 查看文档,我可以看到区域类型将返回包含以下类型的所有结果;
*地方

  • sublocality
  • 邮政编码
    *国家
  • 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);

1 回答

  • 0

    你可以过滤你的查询预测结果,只显示(或做任何你喜欢的事情,因为我们不知道你想要做什么)预测,如果它的类型为 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个结果,因此最终用户可能会感到困惑,因为在您输入相当多的字母之前根本没有显示...

相关问题