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

Javascript 如何使用google api从所选国家/地区查找地址,javascript,google-places-api,Javascript,Google Places Api,目前我正在使用GooglePlaceAPI作为地址 我完全明白了 但现在我想把它重新放到特定的城市 这是我的密码 // Create the autocomplete object, restricting the search to geographical // location types. autocomplete = new google.maps.places.Autocomplete( (document.getElementById('autocomplete')),

目前我正在使用GooglePlaceAPI作为地址 我完全明白了

但现在我想把它重新放到特定的城市

这是我的密码

// 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;
          }
        }
      }
现在我想要的是,如果我从选择框中选择印度,那么我的文本框中应该只显示印度地址

有人知道如何限制这一点吗

您可以使用componentRestrictions


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

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

如果我的答案解决了你的问题,请考虑接受它,我正试图在这个平台上活跃起来。谢谢.var selectedCountry$country.changefunction{selectedCountry=$country选项:selected.val;console.logselectedCountry;};options.componentRestrictions.country=selectedCountry;当我改变国家时,这对我不起作用
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