现在的位置: 首页 > 综合 > 正文

Google Geocoding API—地理编码与反向地理编码

2013年08月11日 ⁄ 综合 ⁄ 共 5495字 ⁄ 字号 评论关闭

什么是地理编码

地理编码是将地址(如“1600 Amphitheatre Parkway, Mountain View, CA”)转换为地理坐标(如纬度 37.423021 和经度 -122.083739)的过程,您可以根据该地理坐标放置标记或定位地图。Google Geocoding API 可让您通过 HTTP 请求直接访问地理编码器。此外,该服务还可让您执行反向操作(将坐标转换为地址);此过程称为“反向地理编码”。

使用限制

使用 Google Geocoding API 时,每天发送的地理位置查询请求不得超过 2,500 个。(Google Maps API for Business 用户每天最多可执行 100,000 个请求。)执行此限制是为了防止滥用和/或出于其他目的使用 Geocoding API,并且此限制以后可能会在不另作通知的情况下发生变化。此外,我们还设定了请求速率限制,以防滥用此服务。如果您超出了 24 小时限制或以其他方式滥用此服务,Geocoding API 可能会临时停止运行。如果您继续超出此限制,那么系统将阻止您访问
Geocoding API。

注意:Geocoding API 只能与 Google 地图配合使用;不得只进行地理编码而不在地图上显示结果。有关允许的用法的完整详情,请参阅 Maps
API 服务条款许可限制

地理编码请求

Geocoding API 请求必须采用以下形式:

http://maps.googleapis.com/maps/api/geocode/output?parameters

其中,output 可以是以下两个值中的任意一个:

  • json(推荐),用于表示以 JavaScript 对象表示法 (JSON) 的形式输出
  • xml,用于表示以 XML 的形式输出

要通过 HTTPS 访问 Geocoding API,请使用以下形式:

https://maps.googleapis.com/maps/api/geocode/output?parameters

如果应用包含敏感的用户数据(例如用户所处位置),建议您在请求中使用 HTTPS。

在这两种情况中,有些参数是必填的,有些则是可选的。根据网址的标准,所有参数均使用和号字符 (&) 进行分隔。下面的列表枚举了这些参数及其可能的值。

必填参数

  • address:要进行地理编码的地址。 
          
    latlng:您希望获取的距离最近、便于用户理解的地址的纬度/经度文本值。有关详情,请参阅反向地理编码。 
          
    components:您希望为其获取地理编码的组成部分过滤器。有关详情,请参阅组成部分过滤。如果提供了 address,那么也接受作为可选参数的组成部分过滤器。
  • sensor:用于表示地理编码请求是否来自装有位置传感器的设备。该值只能是 true 或 false

Maps API for Business 用户必须在地理编码请求中添加有效的 client 和 signature 参数。有关详情,请参阅 Maps
API for Business 网络服务

可选参数

  • bounds:要在其中更明显偏向地理编码结果的视口的边框。该参数只会对地理编码器返回的结果产生影响,而不能对其进行完全限制。(有关详情,请参阅下面的视口偏向。)
  • language,即返回结果时所使用的语言。请参阅支持的区域语言列表。请注意,我们会经常更新支持的语言,因此该列表可能并不详尽。如果未提供 language,那么地理编码器将尝试尽可能使用发送请求的区域的本地语言。
  • region:即区域代码,已指定为 ccTLD(“顶级域”)双字符值。该参数只会对地理编码器返回的结果产生影响,而不能对其进行完全限制。(有关详情,请参阅下面的区域偏向。)
  • components:组成部分过滤器,以竖线 (|)
    进行分隔。每个组成部分过滤器均由一对 component:value 构成,并且会完全限制地理编码器返回的结果。有关详情,请参阅下面的组成部分过滤

地理编码响应

返回的地理编码响应格式由网址请求的路径中的 output 标记指定。

JSON 输出格式

在此示例中,Geocoding API 针对关于“1600 Amphitheatre Parkway, Mountain View, CA”的查询请求了一个 json 响应:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

在此示例中,我们将 sensor 参数设为变量“true_or_false”,以强调您必须将该值明确设置为 true 或 false

此请求返回的 JSON 如下所示:请注意,实际的 JSON 可能包含较少的空白。您不应假定请求之间的空白数量或格式。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "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.42291810,
               "lng" : -122.08542120
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42426708029149,
                  "lng" : -122.0840722197085
               },
               "southwest" : {
                  "lat" : 37.42156911970850,
                  "lng" : -122.0867701802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

请注意,JSON 响应包含两个根元素:

  • "status",其中包含请求中的元数据。请参阅下面的状态代码
  • "results",其中包含一个经过地理编码的地址信息和几何图形信息的数组。

对于地址查询,地理编码器通常只会返回 "results" 数组中的一个条目,不过在进行模糊地址查询时,可能会返回多个结果。

请注意,如果您希望从结果中提取值,那么通常需要对这些结果进行解析。解析 JSON 相对来说较容易。有关一些建议设计模式,请参阅解析
JSON

XML 输出格式

在此示例中,Google Geocoding API 针对上述同一关于“1600 Amphitheatre Parkway, Mountain View, CA”的查询请求了一个 xml 响应:

http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

此请求返回的 XML 如下所示:

<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>street_address</type>
  <formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
  <address_component>
   <long_name>1600</long_name>
   <short_name>1600</short_name>
   <type>street_number</type>
  </address_component>
  <address_component>
   <long_name>Amphitheatre Pkwy</long_name>
   <short_name>Amphitheatre Pkwy</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Mountain View</long_name>
   <short_name>Mountain View</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>San Jose</long_name>
   <short_name>San Jose</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Santa Clara</long_name>
   <short_name>Santa Clara</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>California</long_name>
   <short_name>CA</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>United States</long_name>
   <short_name>US</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>94043</long_name>
   <short_name>94043</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>37.4217550</lat>
    <lng>-122.0846330</lng>
   </location>
   <location_type>ROOFTOP</location_type>
   <viewport>
    <southwest>
     <lat>37.4188514</lat>
     <lng>-122.0874526</lng>
    </southwest>
    <northeast>
     <lat>37.4251466</lat>
     <lng>-122.0811574</lng>
    </northeast>
   </viewport>
  </geometry>
 </result>
</GeocodeResponse>

请注意,该 XML 响应包括一个 <GeocodeResponse> 和以下两个顶级元素:

  • <status>,其中包含请求中的元数据。请参阅下面的状态代码
  • <result> 元素(不包括或包括多个),每个该元素均包含一组经过地理编码的地址信息和几何图形信息。

请注意,此响应所花费的时间远远多于 JSON 响应。因此,除非您的服务出于某些原因需要 json,否则建议您将 xml 用作首选的输出标记。此外,请谨慎处理
XML 树,以便引用正确的节点和元素。有关用于输出处理的一些建议设计模式,请参阅使用
XPath 解析 XML

本文档的其余部分将会使用 JSON 语法。在大多数情况下,对于说明本文档中的概念或字段名称,输出格式并不是很重要。不过,请注意以下细微差别:

  • XML 结果会包含在一个根 <GeocodeResponse> 元素中。
  • JSON 通

抱歉!评论已关闭.