Reputation:
currently i am using google place api for address and i am getting it perfectly
but now i want to restict it to perticular city
here is my code
// 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;
}
}
}
now all i want that if i select "INDIA" from select box only indian address should appear on my text box
can any one has idea about how to restrict this
Upvotes: 1
Views: 93
Reputation: 864
add restrictions on Autocomplete while creating its object.
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;
}
}
}
As the value of country changes, change the value of
options.componentRestrictions.country
Upvotes: 2
Reputation: 3570
You can use componentRestrictions
{types: ['geocode'],componentRestrictions: {country: 'IN'}}
Upvotes: 0