目前我正在使用google place api获取地址 我完美地完成了它
但现在我想把它重新定位到特定的城市
这是我的代码
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
现在我想要的是,如果我从选择框中选择“印度”,只有印度地址应出现在我的文本框
任何人都可以知道如何限制这个
答案 0 :(得分:2)
在创建对象时添加对自动填充的限制。
var options = {
types: ['geocode'],
componentRestrictions: {country: "IN"}
};
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
options);
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
随着国家/地区价值的变化,请更改
的值options.componentRestrictions.country
答案 1 :(得分:0)
您可以使用componentRestrictions
{types: ['geocode'],componentRestrictions: {country: 'IN'}}