Google地理编码 - 如何使用REST api从lat,lng获取引用或id

时间:2014-05-25 10:36:30

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

我已经在这个问题上拖了几天...... 我使用Node.js对谷歌地图REST API进行http调用 我需要查看我的数据库中的位置记录,并使用lat,lng - 在给定的分辨率中找到谷歌的位置ID或引用 - 比如" country"或" administrative_level_1"例如......

我发现这两个休息电话最接近我的需要:

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对我来说更好。它更好,因为我试图处理的位置是多种语言,为每个记录指定其语言将是一场噩梦。

这是前一个电话的结果: 我需要迭代预测对象,并在我正在寻找的分辨率中找到了...

{
   "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,因此我需要将这些转换为谷歌位置ID仅使用lat,lng和分辨率...名称是我无法控制的他们是一个给定的。并以多种语言......

如果你能指出一个能够做到这一点的REST API调用或一系列http调用,我会很感激...

欣赏它。 Ajar

1 个答案:

答案 0 :(得分:0)

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

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