【问题标题】:Google geocoding - how to get reference or id from lat,lng using REST apiGoogle 地理编码 - 如何使用 REST api 从 lat、lng 获取参考或 id
【发布时间】:2014-07-14 07:08:51
【问题描述】:

这几天我一直在努力解决这个问题...... 我正在使用 Node.js 对 Google 地图 REST API 进行 http 调用 我需要查看我的数据库中的位置记录,并使用 lat、lng - 在给定的分辨率中查找 google 位置 ID 或参考 - 例如说“国家”或“管理级别_1”...

我发现这两个 rest 调用最接近我的需要:

https://maps.googleapis.com/maps/api/geocode/json?
latlng=47.333333,13.333333
&sensor=false
&key=YOUR_API_KEY
&result_type=administrative_area_level_1 

此调用接受 lat、lng 和所需的分辨率并返回准确的结果,但没有 id 或参考... 这是结果json:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Salzburg",
               "short_name" : "Sbg.",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Austria",
               "short_name" : "AT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Salzburg, Austria",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 48.04121,
                  "lng" : 13.9965099
               },
               "southwest" : {
                  "lat" : 46.94347,
                  "lng" : 12.07669
               }
            },
            "location" : {
               "lat" : 47.80949,
               "lng" : 13.05501
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 48.04121,
                  "lng" : 13.9965099
               },
               "southwest" : {
                  "lat" : 46.94347,
                  "lng" : 12.07669
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      }
   ],
   "status" : "OK"
}

到目前为止,我唯一的其他选择是使用自动完成 REST API,如下所示:

https://maps.googleapis.com/maps/api/place/autocomplete/xml?
key=YOUR_API_KEY
&location=47.333333,13.333333
&sensor=false
&language=en
&types=(regions)
&input=Austria

这个问题在于,由于它是用于自动完成目的,因此需要传入“输入”参数,而不仅仅是 lat,lng,这对我来说更好。这样做会更好,因为我要处理的位置有多种语言,为每条记录指定其语言将是一场噩梦。

这是上一次调用的结果 json: 我需要迭代预测对象并在我正在寻找的分辨率中找到那些...

{
   "predictions" : [
      {
         "description" : "Austria",
         "id" : "e16f8e9f2a60f60a0881c41488ff3869e4f938a4",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "CiQfAAAAF11VDI_p7OWWL8woW8cOfCDDeA0iE-NMQwQsbQhouSASECau8aTrkIVsiUcGrmnNHhwaFEmcUvCjjjsHRDznswqC_xeOYiBR",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Austria"
            }
         ],
         "types" : [ "country", "political", "geocode" ]
      },
      {
         "description" : "Lower Austria, Austria",
         "id" : "03ea5cab0b5abe4cb16f66e9a42c27d040ce2b3c",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 6
            }
         ],
         "reference" : "CjQuAAAAznpNXppnhg3bl-36bfMr4tsQa3ZjJJWmtUuHIEzEy4RL9M_LeSGc8muP-_h_e89gEhCFjfuwyex9OA36skw8bD45GhQ14NncFQk-AHOILKC4IqEPe-XqDw",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Lower Austria"
            },
            {
               "offset" : 15,
               "value" : "Austria"
            }
         ],
         "types" : [ "administrative_area_level_1", "political", "geocode" ]
      },
      {
         "description" : "Vienna, Austria",
         "id" : "f622c175d668fbf75e7488ddf7b1a04ecb667788",
         "matched_substrings" : [
            {
               "length" : 6,
               "offset" : 0
            }
         ],
         "reference" : "CjQnAAAAD88YSerev6ZNhRw6hI0P9g_Brsjzc_qN5PqKtugj7GXIMbhqKK1qvJXYprMFu67mEhDYSzeYQwEQFjnOxjb3SsxAGhTocUQpKtR1CoM7sYJJROGImDMQzQ",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Vienna"
            },
            {
               "offset" : 8,
               "value" : "Austria"
            }
         ],
         "types" : [ "locality", "political", "geocode" ]
      },
      {
         "description" : "Oberosterreich, Austria",
         "id" : "5ecb86bf53005b6cd7499f660e2312089a955ced",
         "matched_substrings" : [
            {
               "length" : 14,
               "offset" : 0
            }
         ],
         "reference" : "CjQvAAAAgtYVlhBFCDPBgcJu_7CXHJ8ncd3m6IozPSg3YzaLL4kXVtQowpjx4kc2K-Tqm_0YEhCn4rEiM0MaChwBhkki0qu_GhRgpGKjjh4Hq4aN2n9jFjdfc-Xl8Q",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Oberosterreich"
            },
            {
               "offset" : 16,
               "value" : "Austria"
            }
         ],
         "types" : [ "administrative_area_level_1", "political", "geocode" ]
      },
      {
         "description" : "Austrian District, Gyumri, Shirak, Armenia",
         "id" : "3948451df8a40142c1adeeeb4a969dc1c7d82d39",
         "matched_substrings" : [
            {
               "length" : 7,
               "offset" : 0
            }
         ],
         "reference" : "ClRCAAAANrMT8rQH819b7rtJiCbaq0XbDip8VuQCO7EvoIkVmg4xUyQtxGiFhm9sF52hTbtp8PjR-b0_dCLn-ZXceYfJJ64j1hwxbuthSHomBoGBphMSEPC8ZwPtn6dOrYb_duar-tcaFIFbhTX9HWS4UDzF2exZcw2YUh9o",
         "terms" : [
            {
               "offset" : 0,
               "value" : "Austrian District"
            },
            {
               "offset" : 19,
               "value" : "Gyumri"
            },
            {
               "offset" : 27,
               "value" : "Shirak"
            },
            {
               "offset" : 35,
               "value" : "Armenia"
            }
         ],
         "types" : [ "sublocality", "political", "geocode" ]
      }
   ],
   "status" : "OK"
}

原始数据来自我们使用的地名数据库。我们正在切换到在我们应用程序的所有部分中使用谷歌地图 API,因此我需要仅使用 lat、lng 和分辨率将它们转换为谷歌位置 ID……名称是我无法控制的,因为它们是给定的。并且有多种语言...

如果您能指出一个能够执行此操作的 REST API 调用或一系列 http 调用,我将不胜感激...

欣赏它。 半开

【问题讨论】:

    标签: node.js api google-maps rest google-maps-api-3


    【解决方案1】:

    基于 Google 网站上的 API picker,我建议使用以下 API:

    将地理位置的纬度/经度坐标转换为地址(反向地理编码)。
    Google Places API(网络服务)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      相关资源
      最近更新 更多