将 Google 地图地址查找限制为特定邮政编码

Limit Google Maps address lookup to a particular postcode

如何将 Google 地图地址查询服务限制为特定邮政编码或 city/town 中的街道名称。它目前为我提供了英国的所有地址,但我想将其限制为仅在英国伯明翰的地址(B 邮政编码)。

到目前为止的代码

JS

$(document).ready(function () {
    addressLookup();
});

function addressLookup() {
      var options = {
        componentRestrictions: {
            country: 'uk'
        }
    };
    var address = document.getElementsByClassName('form-control booking address');


    for(var i=0; i< address.length; i++){
    new google.maps.places.Autocomplete(address[i], options);
    }
        //new google.maps.places.Autocomplete(address, options);
}

HTML

<input type="text" class="form-control booking address" id="pickup" placeholder="From">
<input type="text" class="form-control booking address" id="destination" placeholder="To">

Fiddle here

例如,如果伯明翰邮政编码的定义以 B 开头,并且我假设您正在循环的 address 数组中的每个值都是邮政编码(是吗?):

function isBirmingham(postcode) {
    return postcode.substr(0, 1) === 'B';
}

function addressLookup() {
    var options = {
        componentRestrictions: {
            country: 'uk'
        }
    };
    var address = document.getElementsByClassName('form-control booking address');

    for (var i = 0; i < address.length; i++) {
        if (isBirmingham(address[i])) {
            new google.maps.places.Autocomplete(address[i], options);
        }
    }
}

更新:

在自动完成选项中设置 componentRestrictions 怎么样?请参阅 https://developers.google.com/maps/documentation/javascript/reference#AutocompleteOptions - the documentation only mentions you can specify a country. However this article 演示如何使用邮政编码。