使用 Google 设置 componentRestrictions 放置自动完成 API

Set componentRestrictions with Google Place Autocomplete API

我正在制作一个自定义自动完成字段,我的目标是仅从一个国家/地区获取城市。

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amsterdam&inputtype=textquery&types=(cities)&fields=geometry,name&language=nl&key=APIKEY

我已阅读有关绑定特定国家/地区的 componentRestrictions 的信息。如何在请求中包含参数?

添加参数 &componentRestrictions=nl 无效。

Web 服务的每个 the documentation 参数为 components:

  • components — A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by up to 5 countries. Countries must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France. Multiple countries must be passed as multiple country:XX filters, with the pipe character (|) as a separator. For example: components=country:us|country:pr|country:vi|country:gu|country:mp would restrict your results to places within the United States and its unincorporated organized territories.

示例:components=country:nl

完整 URL:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amsterdam&inputtype=textquery&types=(cities)&fields=geometry,name&language=nl&components=country:nl&key=APIKEY