Reputation: 8834
I am using google maps autocomplete correctly, however I want to restrict all the places shown to a specific country.
What modifications shall I do into the code in order to achieve that?
function initAutocomplete() {
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{types: ['geocode']});
}
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
})
;
}
}
Upvotes: 2
Views: 7799
Reputation: 75
just try adding it after '{types:[geocode]}'
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{types: ['geocode'], componentRestrictions: {country: 'fr'}});
Upvotes: 0
Reputation: 976
Use the componentRestrictions option to restrict the autocomplete search to a particular country. The following code restricts the results to cities within France.
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'fr'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
Upvotes: 10