在谷歌地图中获取地址和邮政编码以及经度和纬度

2023-12-20

在谷歌地图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/ https://developers.google.com/maps/documentation/geocoding/


您可以通过搜索获得邮政编码postal_code type:

    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");
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在谷歌地图中获取地址和邮政编码以及经度和纬度 的相关文章

随机推荐