Google Maps Geocoding API:如何以英语或本地语言一致地获取城市/地区名称*?

时间:2015-09-07 16:57:12

标签: internationalization reverse-geocoding google-geocoding-api

当我提出以下请求(请注意language=en部分!)时,纬度为&华沙,波兰的经度,

https://maps.googleapis.com/maps/api/geocode/json?latlng=52.23,21.01&language=en

我得到一个包含以下内容的结果列表:

结果3(邻里):

    {
            "address_components" : [
               {
                  "long_name" : "Śródmieście Północne",
                  "short_name" : "Śródmieście Północne",
                  "types" : [ "neighborhood", "political" ]
               },
               {
                  "long_name" : "Śródmieście",
                  "short_name" : "Śródmieście",
                  "types" : [ "sublocality_level_1", "sublocality", "political" ]
               },
               {
                  "long_name" : "Warszawa",   <-- city in Polish
                  "short_name" : "Warszawa",
                  "types" : [ "locality", "political" ]
               },
               {
                  "long_name" : "Warszawa",
                  "short_name" : "Warszawa",
                  "types" : [ "administrative_area_level_3", "political" ]
               },
               {
                  "long_name" : "Warszawa",
                  "short_name" : "Warszawa",
                  "types" : [ "administrative_area_level_2", "political" ]
               },
               {
                  "long_name" : "mazowieckie",  <-- region in Polish
                  "short_name" : "mazowieckie",
                  "types" : [ "administrative_area_level_1", "political" ]
               },
               {
                  "long_name" : "Poland",
                  "short_name" : "PL",
                  "types" : [ "country", "political" ]
               }
            ],
         [...]
         "place_id" : "ChIJjaxkh4rMHkcR1lza9qbbnrU",
         "types" : [ "neighborhood", "political" ]
    }

结果4(sublocality_level_1):

      {
         "address_components" : [
            {
               "long_name" : "Śródmieście",
               "short_name" : "Śródmieście",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "Warsaw",   <-- city in English
               "short_name" : "Warsaw",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Warszawa",
               "short_name" : "Warszawa",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Warszawa",
               "short_name" : "Warszawa",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Masovian Voivodeship",  <-- region in English
               "short_name" : "Masovian Voivodeship",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Poland",
               "short_name" : "PL",
               "types" : [ "country", "political" ]
            }
         ],
         [...]
         "place_id" : "ChIJ8e41PvbMHkcRit4n8qe36Ns",
         "types" : [ "sublocality_level_1", "sublocality", "political" ]
      },

这个问题的主要问题是相同地址组件(locality,administrative_area_level_1)的不一致 - 有时是英语(根据要求),有时是本地语言。如果没有其他ID知道这些是指相同的行政现实,我无法确定这一点,除非我使用了另一个英语和其他来源。本地地理名称并进行了一些模式匹配。

有没有办法解决这个问题?或者有人建议反向地理编码服务没有这样的问题?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我写了一个if/else组来按优先顺序获取位置数据。

foreach ($data->results[0]->address_components as $stuff) {
    // city
    if ($stuff->types[0] == 'locality') {
        $zip->locality_short = $stuff->short_name;
        $zip->locality_long = $stuff->long_name;
    } elseif ($stuff->types[0] == 'sublocality_level_1') {
        $zip->locality_short = $stuff->short_name;
        $zip->locality_long = $stuff->long_name;
    } elseif ($stuff->types[0] == 'neighborhood') {
        $zip->locality_short = $stuff->short_name;
        $zip->locality_long = $stuff->long_name;
    }
    // state
    if ($stuff->types[0] == 'administrative_area_level_1') {
        $zip->location_short = $stuff->short_name;
        $zip->location_long = $stuff->long_name;
    }
    // country
    if ($stuff->types[0] == 'country') {
        $zip->country_short = $stuff->short_name;
        $zip->country_long = $stuff->long_name;
    }
}