开始使用

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

您可以通过在网络浏览器中输入网址来测试此功能(请务必将 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

您可以通过在网络浏览器中输入网址来测试此功能(请务必将“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 可从 Java 客户端、Python 客户端、Go 客户端和 Node.js 客户端(适用于 Google 地图服务)获得。

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

身份验证

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

配额和价格

查看使用情况和结算页面,详细了解为 Geocoding API 设置的配额和价格。

政策

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

了解详情

您可以利用 Geocoding API 执行更多操作。有关其他演示、示例、可用参数、状态代码、错误消息以及其他详细信息,请参阅 Geocoding API 开发者指南

Geocoding API 开发者指南介绍了 Geocoding API 网络服务。其适用对象是想要在其中一个 Google Maps Platform API 提供的地图中使用地理编码数据的网站和移动开发者。