开始使用

Geocoding API 是一项提供地址地理编码和反向地理编码的服务。

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

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

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

示例请求和响应

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

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

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

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

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

请查看开发者指南,详细了解如何 构建地理编码请求网址可用参数以及 理解响应

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

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4267861,
               "lng" : -122.0806032
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4281350802915,
                  "lng" : -122.0792542197085
               },
               "southwest" : {
                  "lat" : 37.4254371197085,
                  "lng" : -122.0819521802915
               }
            }
         },
         "place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
         "plus_code" : {
            "compound_code" : "CWC8+R3 Mountain View, California, United States",
            "global_code" : "849VCWC8+R3"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

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

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

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

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

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

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

{
   "plus_code" : {
      "compound_code" : "P27Q+MC New York, NY, USA",
      "global_code" : "87G8P27Q+MC"
   },
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "279",
               "short_name" : "279",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Kings County",
               "short_name" : "Kings County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "279 Bedford Ave, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.7142484,
               "lng" : -73.9614103
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.71559738029149,
                  "lng" : -73.9600613197085
               },
               "southwest" : {
                  "lat" : 40.71289941970849,
                  "lng" : -73.96275928029151
               }
            }
         },
         "place_id" : "ChIJT2x8Q2BZwokRpBu2jUzX3dE",
         "plus_code" : {
            "compound_code" : "P27Q+MC Brooklyn, New York, United States",
            "global_code" : "87G8P27Q+MC"
         },
         "types" : [
            "bakery",
            "cafe",
            "establishment",
            "food",
            "point_of_interest",
            "store"
         ]
      },

   ... Additional results truncated in this example[] ...

   ],
   "status" : "OK"
}

使用客户端库编码入门指南

客户端库通过以简单、原生的方式实现身份验证、请求限制和自动重试等常见任务,简化了 Google 地图网络服务 API 的开发工作。Geocoding API 可在适用于 Google 地图服务的 Java 客户端、Python 客户端、Go 客户端和 Node.js 客户端中使用。

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

身份验证

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

配额和价格

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

政策

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

了解详情

Geocoding API 还有其他用途。有关其他演示、示例、可用参数、状态代码和错误消息以及其他详情,请参阅 Geocoding API 开发者指南

Geocoding API 开发者指南介绍了 Geocoding API 网络服务。适用于希望在某个 Google Maps Platform API 提供的地图中使用地理编码数据的网站和移动开发者。