简体   繁体   English

在Google地图中获取具有经度和纬度的地址和邮政编码

[英]get address and zipcode with longitude and latitude in google map

in google map api, i want to extract map center latitude and longitude and get address of there like a zipcode. 在谷歌地图API中,我想提取地图中心的纬度和经度,并像邮政编码一样获取那里的地址。 this is possible to get zipcode ? 这有可能获得邮政编码吗?
i use this for this purpose 我为此目的使用它

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);
            }
        });

but in above code i can't get zipcode and get postal code! 但是在上面的代码中,我无法获取邮政编码和邮政编码!
https://developers.google.com/maps/documentation/geocoding/ https://developers.google.com/maps/documentation/geocoding/

You can get postal code searching for postal_code type: 你可以得到邮政编码搜索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");
        }
    }

As far as I'm aware, you've answered your own question here. 据我所知,您已经在这里回答了自己的问题。 The 'Postal Code' is the generic name given to the mailing address identifier used in each country. “邮政编码”是每个国家/地区使用的邮寄地址标识符的通用名称。 In the case of the US this would be the 'Zip Code', in the UK the 'Post Code' etc. 在美国,这是“邮政编码”,在英国是“邮政编码”等。

From the documentation you linked: 从您链接的文档中:

postal_code indicates a postal code as used to address postal mail within the country. postal_code表示用于处理该国家/地区内的邮政邮件的邮政编码。

The field under Postal Code is likely what you're looking for, don't let the naming convention fool you. 邮政编码下的字段可能是您要查找的内容,不要让命名约定欺骗您。 It would be unnecessary work on the part of Google to rename the field for each country's possible variations. Google不必为每个国家/地区的可能变化而对字段进行重命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM