Javascript 谷歌地图API搜索特定国家的邮政编码

Javascript 谷歌地图API搜索特定国家的邮政编码,javascript,google-maps,google-maps-api-3,google-maps-markers,google-geocoder,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,Google Geocoder,我正试图设置一个邮政编码,以便获得横向和纵向坐标,并在其上放置一个标记。直到现在,一切都很好 当我输入一个邮政编码,它最终在世界的另一个地方做了一个标记时,问题就来了 例:我键入2975-435,得到: 我想让这个邮政编码只在葡萄牙搜索 这样我得到: "formatted_address" : "2975 Q.ta do Conde, Portugal", 正是我想要的 问题是,如何在JS代码中实现这一点? 这是我到现在为止的密码 function codeAddress () {

我正试图设置一个邮政编码,以便获得横向和纵向坐标,并在其上放置一个标记。直到现在,一切都很好

当我输入一个邮政编码,它最终在世界的另一个地方做了一个标记时,问题就来了

例:我键入2975-435,得到:

我想让这个邮政编码只在葡萄牙搜索

这样我得到:

"formatted_address" : "2975 Q.ta do Conde, Portugal",
正是我想要的

问题是,如何在JS代码中实现这一点? 这是我到现在为止的密码

function codeAddress () {
    var lat = '';
    var lng = '';
    var address = document.getElementById("cp").value;
    geocoder.geocode( { 'address': address},

    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat();
            lng = results[0].geometry.location.lng();
            //Just to keep it stored
            positionArray.push(new google.maps.LatLng(lat,lng));
            //Make the marker
            new google.maps.Marker({
                position:new google.maps.LatLng(lat,lng),
                map:map
            });

        }else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

谢谢

要将结果限制在特定国家/地区,您可以应用组件筛选:

因此,您的JavaScript代码将

function codeAddress () {
    var lat = '';
    var lng = '';
    var address = document.getElementById("cp").value;
    geocoder.geocode( { 
        'address': address,
        componentRestrictions: {
            country: 'PT'
        }
    },

    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat();
            lng = results[0].geometry.location.lng();
            //Just to keep it stored
            positionArray.push(new google.maps.LatLng(lat,lng));
            //Make the marker
            new google.maps.Marker({
                position:new google.maps.LatLng(lat,lng),
                map:map
            });

        }else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
您可以看到使用Geocoder工具进行过滤的组件:


希望有帮助

要将结果限制在特定国家/地区,您可以应用组件筛选:

因此,您的JavaScript代码将

function codeAddress () {
    var lat = '';
    var lng = '';
    var address = document.getElementById("cp").value;
    geocoder.geocode( { 
        'address': address,
        componentRestrictions: {
            country: 'PT'
        }
    },

    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            lat = results[0].geometry.location.lat();
            lng = results[0].geometry.location.lng();
            //Just to keep it stored
            positionArray.push(new google.maps.LatLng(lat,lng));
            //Make the marker
            new google.maps.Marker({
                position:new google.maps.LatLng(lat,lng),
                map:map
            });

        }else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
您可以看到使用Geocoder工具进行过滤的组件:

希望有帮助