Google 地图 API - 按邮政编码搜索

Google map API - Search by Postcode

我正在使用 Google 地图 Javascript API 并且工作正常。

 var geocoder = new google.maps.Geocoder();

geocoder.geocode({ 'address': address }, function (results, status) {
  if (status == 'OK') {
    map.setCenter(results[0].geometry.location);

唯一的问题是我不能只按邮政编码搜索。例如在澳大利亚,如果我只搜索悉尼邮政编码 2000(地址 = 2000),它不会 return 任何结果,但如果我转到 Google 地图页面并输入 2000,它会显示正确的区域。 我想知道有没有办法通过邮政编码搜索

你试过先限制国家吗?

试试这个让我知道:

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