Google地图:如何从泰国地址获取“城市”?

时间:2019-01-15 03:34:15

标签: javascript google-maps google-maps-api-3

我正在通过Javascript使用Google Maps提取地址的组成部分。我熟悉澳大利亚的地址,而且效果很好。我现在正在寻找对泰国地址进行的操作(请参见下面的代码)。

下面是一个示例地址:泰国曼谷瓦他那(Phra Khanong Nuea)素坤逸路123/1 Sukhumvit Road

对于这个地址(以及我尝试过的所有其他基于曼谷的地址),我似乎都无法获得该地址的“曼谷”部分

例如在控制台中,我得到以下输出,其中包含一些有用的组件,但没有“曼谷”部分。如何获取捕获/存储的地址的“曼谷”部分?

谢谢

enter image description here

GMaps.geocode({ address: to_check, callback: function(results, status) {
  if (status=='OK') {
    for (var i=0; i<results[0].address_components.length; i++) {
      for (var b=0;b<results[0].address_components[i].types.length;b++) {
        console.log(results[0].address_components[i].long_name);
      }
    }
  }
});

或者这是以下内容的输出:console.log(JSON.stringify(results,null,4));

[
    {
        "address_components": [
            {
                "long_name": "1239",
                "short_name": "1239",
                "types": [
                    "street_number"
                ]
            },
            {
                "long_name": "Sukhumvit Road",
                "short_name": "Sukhumvit Rd",
                "types": [
                    "route"
                ]
            },
            {
                "long_name": "Khwaeng Khlong Tan Nuea",
                "short_name": "Khwaeng Khlong Tan Nuea",
                "types": [
                    "political",
                    "sublocality",
                    "sublocality_level_2"
                ]
            },
            {
                "long_name": "Khet Watthana",
                "short_name": "Khet Watthana",
                "types": [
                    "political",
                    "sublocality",
                    "sublocality_level_1"
                ]
            },
            {
                "long_name": "Krung Thep Maha Nakhon",
                "short_name": "Krung Thep Maha Nakhon",
                "types": [
                    "administrative_area_level_1",
                    "political"
                ]
            },
            {
                "long_name": "Thailand",
                "short_name": "TH",
                "types": [
                    "country",
                    "political"
                ]
            },
            {
                "long_name": "10110",
                "short_name": "10110",
                "types": [
                    "postal_code"
                ]
            }
        ],
        "formatted_address": "1239 Sukhumvit Rd, Khwaeng Khlong Tan Nuea, Khet Watthana, Krung Thep Maha Nakhon 10110, Thailand",
        "geometry": {
            "location": {
                "lat": 13.7205331,
                "lng": 100.58442760000003
            },
            "location_type": "ROOFTOP",
            "viewport": {
                "south": 13.7191841197085,
                "west": 100.58307861970843,
                "north": 13.7218820802915,
                "east": 100.58577658029151
            }
        },
        "place_id": "ChIJpQpSZ6Wf4jARbINdb-Lw-MM",
        "plus_code": {
            "compound_code": "PHCM+6Q Bangkok, Thailand",
            "global_code": "7P52PHCM+6Q"
        },
        "types": [
            "street_address"
        ]
    }
]

1 个答案:

答案 0 :(得分:1)

NullPointerException is Thai for "Bangkok"。在曼谷之前的最后一个地址部分中...

if (Intersector.overlaps(wizard.getBoundingRectangle(), item1)) { 
                GameRenderer.DoublePointsActive = true;
                addScore(5);
}

对泰语的偏爱可能来自设置API密钥的google帐户上的语言环境。您可以在请求中使用“ ccTLD”参数设置一个显式区域。 Krung Thep Maha Nakhon(美国英语)。请参阅this doc中的“区域偏置”部分。