谷歌地图地理编码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",                  "类型" : [ "邻里", "政治" ]               },               {                  "long_name" : "Śródmieście",                  "short_name" : "Śródmieście",                  "类型" : [ "sublocality_level_1", "次地方性", "政治" ]               },               {                  "long_name" : "华沙",<-波兰语城市                  "short_name" : "华沙",                  "类型" : [ "地方性", "政治" ]               },               {                  "long_name" : "华沙",                  "short_name" : "华沙",                  "类型" : [ "administrative_area_level_3", "政治" ]               },               {                  "long_name" : "华沙",                  "short_name" : "华沙",                  "类型" : [ "administrative_area_level_2", "政治" ]               },               {                  "long_name" : "mazowieckie", <-- 波兰语地区                  "short_name" : "马佐维茨基",                  "类型" : [ "administrative_area_level_1", "政治" ]               },               {                  "long_name" : "波兰",                  "short_name" : "PL",                  "类型" : [ "国家", "政治" ]               }            ],         [...]         "place_id" : "ChIJjaxkh4rMHkcR1lza9qbbnrU",         "类型" : [ "邻里", "政治" ]    }

结果 4 (sublocality_level_1(:

     {         "address_components" : [            {               "long_name" : "Śródmieście",               "short_name" : "Śródmieście",               "类型" : [ "sublocality_level_1", "次地方性", "政治" ]            },            {               "long_name" : "华沙", <-- 城市               "short_name" : "华沙",               "类型" : [ "地方性", "政治" ]            },            {               "long_name" : "华沙",               "short_name" : "华沙",               "类型" : [ "administrative_area_level_3", "政治" ]            },            {               "long_name" : "华沙",               "short_name" : "华沙",               "类型" : [ "administrative_area_level_2", "政治" ]            },            {               "long_name" : "Masovian Voivodeship", <-- 英语地区               "short_name" : "马索维亚省",               "类型" : [ "administrative_area_level_1", "政治" ]            },            {               "long_name" : "波兰",               "short_name" : "PL",               "类型" : [ "国家", "政治" ]            }         ],         [...]         "place_id" : "ChIJ8e41PvbMHkcRit4n8qe36Ns",         "类型" : [ "sublocality_level_1", "次地方性", "政治" ]      },

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

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

非常感谢!

我遇到了同样的问题。我写了一个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;
    }
}

最新更新