I'm using Google Maps API example called 'Retrieving Autocomplete Predictions'. And I can't figure out how to filter it so it only returns places in the USA. I see how I could do it with the other examples...
var input = document.getElementById('searchTextField');
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'us'}
};
autocomplete = new google.maps.places.Autocomplete(input, options);
But I can't seem to figure out how to apply it to my example. Here is my code...
function GetAddressPredictions(Search) {
var displaySuggestions = function (predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
}
predictions.forEach(function (prediction) {
PredictionArray.push(prediction);
});
};
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({input: Search}, displaySuggestions);
}
I tried this but it didn't work.
function GetAddressPredictions(Search) {
var displaySuggestions = function (predictions, status) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
}
predictions.forEach(function (prediction) {
PredictionArray.push(prediction);
});
};
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'us'}
};
var service = new google.maps.places.AutocompleteService();
service.getQueryPredictions({input: Search, options}, displaySuggestions);
}
Any Ideas? Thanks.
It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.
First, enable Google Places API Web Service. Get the API key. You will have to use it in the script tag in html file. Use script file to load the autocomplete class.
we can allow only specific country to autocomplete search in google maps. We are using google map autocomplete for choosing address with latitude and longitude. but if you require to search only one country like in, us, uk etc then you can do it using setComponentRestrictions option in google map.
You are so very close but unfortunately to get this specific behavior you need to use getPlacePredictions()
instead of getQueryPredictions()
. This allows you to use AutocompletionRequest which has componentRestrictions
however QueryAutocompletionRequest does not support componetRestictions
and you must fall back to either using bounds
to limit the scope of the search to a box or location
to bias the search to prefer locations near a point.
Here is the solution with getPlacePredictions()
:
var service = new google.maps.places.AutocompleteService();
var options = {
input: Search,
types: ['(cities)'],
componentRestrictions: {country: 'us'}
};
service.getPlacePredictions(options , displaySuggestions);
You can add location
or bounds
as keys to the options object if you must do this with getQueryPredictions()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With