【问题标题】:How to obtain the zip code from Google Maps Geocoder API [duplicate]如何从 Google Maps Geocoder API 获取邮政编码 [重复]
【发布时间】:2021-06-08 00:44:54
【问题描述】:

我需要使用 Google Maps Geocoder API 从给定地址获取邮政编码。我已经能够获取其他数据,例如经度和纬度,但是获取邮政编码只是有点不同。我试图从中解析邮政编码的 Json 是:

    {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4267861,
               "lng" : -122.0806032
            },

我写的javascript函数是:


        async function getZipCode() {
        const searchedAddress = @Html.Raw(Json.Serialize(ViewData["searchString"]));
        const response = await axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
            params: {
                address: searchedAddress,
                key: [key],
                sensor: 'true_or_false'
            }
        })
            let addressComponents = response.data.results[0].address_components;
            for (var i = 0; i < addressComponents.count; i++) {
                if (addressComponents[i].types == 'postal_code') {
                    let zip = response.data.results[0].address_components[i].short_name;
                    return zip;
                    console.log(zip);
                }
            }

         }

我知道,对于这个json示例中提供的数据,我可以通过

response.data.results[0].address_components[6].short_name;

但情况并非总是如此。有时邮政编码包含在 address_components 的不同迭代中,所以我需要想出一种方法来检查类型,然后获取 short_name。

我拥有的功能没有向控制台返回任何内容。我意识到console.log 语句在“if”语句中,但是对于如何实际获取这些数据却一无所知。

【问题讨论】:

标签: javascript json google-maps


【解决方案1】:

你的尝试已经很接近了。看起来你出错的地方在这里:

if (addressComponents[i].types == 'postal_code') { // ...

当我们知道types 似乎总是一个数组时,您要让解释器弄清楚addressComponents[i].types 是否等于字符串 postal_code。在大多数情况下,不能将数组直接强制转换为字符串。

您实际上想要弄清楚的是字符串postal_code 是否存在在数组中。因此,您可以使用Array.prototype.includes():

if (addressComponents[i].types.includes('postal_code')) { // process the postal code data

除此之外:return 之后的任何内容都不会被执行,因此无论成功与否,您都不会使用您的代码看到来自console.log(zip) 的输出。使用具有可靠语法突出显示的 IDE 可能会通过显示不会以较轻的不透明度执行的代码来提醒您这一点 - 如果您还没有,我建议您立即开始使用。

【讨论】:

  • 非常感谢。这与将 addressComponents.count 更改为 addressComponents.length 一起成功了。
猜你喜欢
  • 2011-11-28
  • 2011-07-31
  • 1970-01-01
  • 2016-03-13
  • 2017-02-15
  • 2018-01-03
  • 2013-06-21
  • 2013-04-08
相关资源
最近更新 更多