gpt4 book ai didi

javascript - 我如何使用google api从选定的国家/地区找到地址

转载 作者:行者123 更新时间:2023-11-30 14:43:59 24 4
gpt4 key购买 nike

目前我正在使用 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;
}
}
}

output现在我想要的是,如果我从选择框中选择“INDIA”,则只有印度地址应该出现在我的文本框中

谁知道如何限制这个

最佳答案

在创建对象时添加对自动完成的限制。

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

随着国家值(value)的变化,改变值(value)

options.componentRestrictions.country

关于javascript - 我如何使用google api从选定的国家/地区找到地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49237412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com