我如何使用 google api 查找所选国家/地区的地址

how can i find address from selected country using google api

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

现在我想要的是,如果我 select "INDIA" 来自 select 框,只有印度地址应该出现在我的文本框

谁知道如何限制这个

您可以使用componentRestrictions

{types: ['geocode'],componentRestrictions: {country: 'IN'}}

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

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

随着country值的变化,改变

的值
options.componentRestrictions.country