迁移到自动补全(新)

欧洲经济区 (EEA) 开发者

简介

Places API 支持地点自动补全(旧版)查询自动补全(旧版) API。如果您熟悉这些 API,那么“自动补全”(新)会进行以下更改:

  • 自动补全(新)使用 HTTP POST 请求。在 HTTP POST 请求中,通过请求正文或标头传递参数。相比之下,对于旧版 API,您可以使用 HTTP GET 请求传递网址参数。
  • 自动补全(新)支持使用 API 密钥OAuth 令牌作为身份验证机制。
  • 在自动补全(新)中,仅支持 JSON 作为响应格式。

下表列出了地点自动补全(旧版)和查询自动补全(旧版)中已针对自动补全(新)重命名或修改的参数,以及不再支持的参数。

当前形参 新增参数 备注
components includedRegionCodes
language languageCode
location locationBias
ipbias 如果您同时省略 locationBiaslocationRestriction,则 API 默认使用 IP 偏向。
offset inputOffset
radius locationBiaslocationRestriction
region regionCode
stricbounds locationRestriction
sessiontoken sessionToken
types includedPrimaryTypes

示例请求

以下示例 GET 请求使用了地点自动补全(旧版)。在此示例中,您请求一个 JSON 响应,其中包含有关名为“Amoeba”且类型为 establishment 的地点的信息,并将所有参数作为网址参数传递:

curl -L -X GET 'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=amoeba&types=establishment&location=37.76999%2C-122.44696&radius=500&key=YOUR_API_KEY'

使用自动补全(新)时,您需要发出 POST 请求,并在 JSON 请求正文或标头中传递所有参数,作为 POST 请求的一部分。

自动填充(新)请求不需要字段掩码,但可以使用字段掩码来过滤响应中的特定结果。例如,以下请求使用字段掩码,因此响应仅包含建议的 suggestions.placePrediction.text

curl -X POST -d '{
  "input": "Amoeba",
  "locationBias": {
    "circle": {
      "center": {
        "latitude": 37.76999,
        "longitude": -122.44696
      },
      "radius": 500.0
    }
  }
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: API_KEY' \
-H 'X-Goog-FieldMask: suggestions.placePrediction.text' \
https://places.googleapis.com/v1/places:autocomplete

如需详细了解如何使用字段掩码,请参阅地点详情(新)