Google Place自动填写邮政​​编码

Ris*_*tha 14 javascript google-maps autocomplete google-maps-api-3

我正在尝试创建一个自动填充文本框,该文本框应该只提供邮政编码.以下是我所遵循的文档:https://developers.google.com/places/webservice/autocomplete#place_types

JSFiddle工作示例就在这里

如果我使用postal_code它不适合我,但当我使用cities它的罚款.如何实现仅包含邮政编码的自动填充功能,我该怎么办?

function postal_code() {
  var input = document.getElementById('field-postal');
  var options = {
    types: ['(postal_code)'],
    componentRestrictions: {
      country: "in"
    }
  }
  var autocomplete = new google.maps.places.Autocomplete(input, options);
}

google.maps.event.addDomListener(window, 'load', postal_code);
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 17

文件是不是很清楚.

  • (regions)类型集合指示Places服务返回与以下类型匹配的任何结果:
    • 局部性
    • sublocality
    • 邮政编码
    • 国家
    • administrative_area_level_1
    • administrative_area_level_2


Iva*_*nta 5

谷歌文档可能有点令人困惑。但我想我找到了答案:

仅通过邮政编码搜索:

types:['postal_code']
Run Code Online (Sandbox Code Playgroud)

要搜索地址(包括邮政编码):

types:['(regions)']
Run Code Online (Sandbox Code Playgroud)

或者

types:['geocode']
Run Code Online (Sandbox Code Playgroud)

请参阅下面来自 Google 的文档

https://developers.google.com/maps/documentation/places/web-service/autocomplete#types

您可以安全地混合地理编码和设施类型。您不能将类型集合(地址、(城市)或(地区))与任何其他类型混合,否则会发生错误。

https://developers.google.com/maps/documentation/places/web-service/supported_types#table3

支持的类型有:

  • geocode指示地点自动完成服务仅返回地理编码结果,而不返回业务结果。通常,您可以使用此请求来消除指定位置可能不确定的结果的歧义。
  • address指示地点自动完成服务仅返回具有精确地址的地理编码结果。通常,当您知道用户将查找完全指定的地址时,您可以使用此请求。
  • establishment指示地点自动完成服务仅返回业务结果。
  • 类型(regions)集合指示 Places 服务返回与以下类型匹配的任何结果:
    • locality
    • sublocality
    • postal_code
    • country
    • administrative_area_level_1
    • administrative_area_level_2
  • 类型(cities)集合指示 Places 服务返回与locality或匹配的结果administrative_area_level_3