得到Lon。纬度。对于给定国家/地区内的给定邮政编码

时间:2017-01-25 14:38:45

标签: javascript google-geolocation

我对Google的地理位置api相当新...

我正在使用以下网址检索给定地址的地理位置信息。

http://maps.googleapis.com/maps/api/geocode/json?address=address_value

地址参数可以包含多个信息。 我对地址参数值的正确格式化的研究总是让我使用postcode, country这样的格式,但这似乎并不总能按预期工作。

我的问题是:我怎样才能始终只获取给定国家/地区的信息(比方说德国)?

示例:

柏林邮政编码范围是10115-14199

当我尝试以下内容时 http://maps.googleapis.com/maps/api/geocode/json?address=10115,%20Germany

我得到了正确的信息(位于德国):

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10115",
               "short_name" : "10115",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Mitte",
               "short_name" : "Mitte",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin",
               "short_name" : "Berlin",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "10115 Berlin, Deutschland",
...

当我试试这个 http://maps.googleapis.com/maps/api/geocode/json?address=10696,%20Germany (这也是德国柏林的现有邮政编码)我在美国获得了一个地址(邮政编码被解释为街道号码)

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "10696",
               "short_name" : "10696",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Berlin Road",
               "short_name" : "Berlin Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Berlin Heights",
               "short_name" : "Berlin Heights",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Berlin Township",
               "short_name" : "Berlin Township",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Erie County",
               "short_name" : "Erie County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "USA",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "44814",
               "short_name" : "44814",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "10696 Berlin Rd, Berlin Heights, OH 44814, USA",

...

1 个答案:

答案 0 :(得分:1)

您需要使用

等组件
http://maps.googleapis.com/maps/api/geocode/json?address=10696&components=country:DE