【问题标题】:get address and zipcode with longitude and latitude in google map在谷歌地图中获取带有经度和纬度的地址和邮政编码
【发布时间】:2014-02-26 09:07:47
【问题描述】:

在谷歌地图 api 中,我想提取地图中心的纬度和经度,并像邮政编码一样获取那里的地址。 这可以得到邮政编码吗?
我用这个来达到这个目的

var lat = -24.448674;
        var lng = 135.684569;
        var geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({'latLng': latlng}, function(results, status)
        {
            if (status == google.maps.GeocoderStatus.OK)
            {
                if (results[1]) {
                    alert(results[1].formatted_address);
                } else {
                    alert("No results found");
                }
            }
            else
            {
                alert("Geocoder failed due to: " + status);
            }
        });

但在上面的代码中,我无法获取邮政编码和邮政编码!
https://developers.google.com/maps/documentation/geocoding/

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    搜索postal_code可以得到邮编:

        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                for (var i = 0; i < results[0].address_components.length; i++) {
                    var types = results[0].address_components[i].types;
    
                    for (var typeIdx = 0; typeIdx < types.length; typeIdx++) {
                        if (types[typeIdx] == 'postal_code') {
                            //console.log(results[0].address_components[i].long_name);
                            console.log(results[0].address_components[i].short_name);
                        }
                    }
                }
            } else {
                console.log("No results found");
            }
        }
    

    【讨论】:

    • 嗨@Anto 几个地址没有'postal_code'数组。这种情况有什么解决办法?
    【解决方案2】:

    据我所知,您已经在这里回答了自己的问题。 “邮政编码”是每个国家/地区使用的邮寄地址标识符的通用名称。在美国,这将是“邮政编码”,在英国,这将是“邮政编码”等。

    从您链接的文档中:

    postal_code 表示邮政编码,用于在国内邮寄邮件。

    邮政编码下的字段可能就是您要查找的内容,不要让命名约定欺骗了您。 Google 没有必要为每个国家/地区的可能变化重命名该字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多