Geocoding API v4 使用入门

欧洲经济区 (EEA) 开发者

Geocoding API v4 是一项服务,可提供地址和地点 ID 的地理编码、反向地理编码以及目的地信息。

地理编码是将地址(例如街道地址)转换为地理坐标(例如纬度和经度)的过程,您可以利用此类坐标在地图上放置标记,或在地图上定位。

反向地理编码是将地理位置坐标转换为直观易懂的地址的过程。

您还可以使用 Geocoding API 查找给定地点 ID 对应的地址。

示例请求和响应

您可以通过 HTTP 接口访问 Geocoding API。以下是地理编码和反向地理编码请求的示例。

地理编码请求和响应(纬度/经度查询)

以下示例请求“1600 Amphitheatre Parkway, Mountain View, CA”的纬度和经度,并指定输出必须采用 JSON 格式。

curl -H "X-Goog-Api-Key: YOUR_API_KEY" \
"https://geocode.googleapis.com/v4beta/geocode/address/1600+Amphitheatre+Parkway,+Mountain+View,+CA"

您可以在网络浏览器中输入相应网址进行测试(请务必将 YOUR_API_KEY 替换为您的实际 API 密钥)。 响应包括地址的纬度和经度。

如需详细了解 构建地理编码请求网址可用参数 了解响应,请参阅开发者指南

下面是一个 JSON 格式的地理编码响应示例:

{
  "results": [
    {
      "place": "//places.googleapis.com/places/ChIJ2Z2idB66j4ARI43TG-9RoCQ",
      "placeId": "ChIJ2Z2idB66j4ARI43TG-9RoCQ",
      "location": {
        "latitude": 37.4438238,
        "longitude": -122.0869895
      },
      "granularity": "ROOFTOP",
      "viewport": {
        "low": {
          "latitude": 37.4424748197085,
          "longitude": -122.0883384802915
        },
        "high": {
          "latitude": 37.445172780291493,
          "longitude": -122.08564051970852
        }
      },
      "formattedAddress": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
      "postalAddress": {
        "regionCode": "US",
        "languageCode": "en",
        "postalCode": "94043-1351",
        "administrativeArea": "CA",
        "locality": "Mountain View",
        "addressLines": [
          "1600 Amphitheatre Pkwy"
        ]
      },
      "addressComponents": [
        {
          "longText": "1600",
          "shortText": "1600",
          "types": [
            "street_number"
          ]
        },
        {
          "longText": "Amphitheatre Parkway",
          "shortText": "Amphitheatre Pkwy",
          "types": [
            "route"
          ],
          "languageCode": "en"
        },
        {
          "longText": "Mountain View",
          "shortText": "Mountain View",
          "types": [
            "locality",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "Santa Clara County",
          "shortText": "Santa Clara County",
          "types": [
            "administrative_area_level_2",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "California",
          "shortText": "CA",
          "types": [
            "administrative_area_level_1",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "United States",
          "shortText": "US",
          "types": [
            "country",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "94043",
          "shortText": "94043",
          "types": [
            "postal_code"
          ]
        },
        {
          "longText": "1351",
          "shortText": "1351",
          "types": [
            "postal_code_suffix"
          ]
        }
      ],
      "types": [
        "premise",
        "street_address"
      ]
    }
  ]
}

反向地理编码请求和响应(地址查询)

以下示例请求美国纽约布鲁克林区中与给定纬度/经度对应的地址。它指定输出必须采用 JSON 格式。

https://geocode.googleapis.com/v4beta/geocode/location/37.4225508,-122.0846338?key=YOUR_API_KEY

您可以通过在网络浏览器中输入相应网址来测试此功能(请务必将“YOUR_API_KEY”替换为您的实际 API 密钥)。 响应包含纬度和经度位置的直观易懂的地址。

如需详细了解 构建反向地理编码请求网址可用参数 了解响应,请参阅开发者指南

下面是一个 JSON 格式的反向地理编码响应示例:

{
  "results": [
    {
      "place": "//places.googleapis.com/places/ChIJHRNUiQK6j4ARJ__Hrbt6qsE",
      "placeId": "ChIJHRNUiQK6j4ARJ__Hrbt6qsE",
      "location": {
        "latitude": 37.4225657,
        "longitude": -122.08465439999999
      },
      "granularity": "ROOFTOP",
      "viewport": {
        "low": {
          "latitude": 37.421214719708509,
          "longitude": -122.08603273029148
        },
        "high": {
          "latitude": 37.423912680291508,
          "longitude": -122.0833347697085
        }
      },
      "bounds": {
        "low": {
          "latitude": 37.4225178,
          "longitude": -122.08484599999998
        },
        "high": {
          "latitude": 37.422609600000008,
          "longitude": -122.0845215
        }
      },
      "formattedAddress": "Google Building 40, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
      "postalAddress": {
        "regionCode": "US",
        "languageCode": "en",
        "postalCode": "94043",
        "administrativeArea": "CA",
        "locality": "Mountain View",
        "addressLines": [
          "Google Building 40",
          "1600 Amphitheatre Pkwy"
        ]
      },
      "addressComponents": [
        {
          "longText": "Google Building 40",
          "shortText": "Google Building 40",
          "types": [
            "premise"
          ],
          "languageCode": "en"
        },
        {
          "longText": "1600",
          "shortText": "1600",
          "types": [
            "street_number"
          ]
        },
        {
          "longText": "Amphitheatre Parkway",
          "shortText": "Amphitheatre Pkwy",
          "types": [
            "route"
          ],
          "languageCode": "en"
        },
        {
          "longText": "Mountain View",
          "shortText": "Mountain View",
          "types": [
            "locality",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "Santa Clara County",
          "shortText": "Santa Clara County",
          "types": [
            "administrative_area_level_2",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "California",
          "shortText": "CA",
          "types": [
            "administrative_area_level_1",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "United States",
          "shortText": "US",
          "types": [
            "country",
            "political"
          ],
          "languageCode": "en"
        },
        {
          "longText": "94043",
          "shortText": "94043",
          "types": [
            "postal_code"
          ]
        }
      ],
      "types": [
        "street_address",
        "subpremise"
      ]
    },
    ... Additional results truncated in this example[] ...
}

身份验证、配额、价格和政策

身份验证

如需使用 Geocoding API,您必须先启用该 API 并获取适当的身份验证凭据。如需了解详情,请参阅 Google Maps Platform 使用入门

配额和价格

如需详细了解为 Geocoding API 设置的配额和价格,请查看用量和结算页面。

政策

使用 Geocoding API 时,必须遵守 API 政策