How to Limit Google Autocomplete Results to City and Country Only

How to limit google autocomplete results to City and Country only

You can try the country restriction

function initialize() {

var options = {
types: ['(cities)'],
componentRestrictions: {country: "us"}
};

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, options);
}

More info:

ISO 3166-1 alpha-2 can be used to restrict results to specific groups. Currently, you can use componentRestrictions to filter by country.


The country must be passed as as a two character, ISO 3166-1 Alpha-2 compatible country code.



Officially assigned country codes

Limit google places autocomplete results to only display state or country or both

Unfortunately, for autocomplete, only the geocode, address, establishment, (regions), and (cities) types can be used.

The only way this problem can really be solved is by using the HTTP API and creating your own UI for the autocomplete box. This link can be used for the api: https://maps.googleapis.com/maps/api/place/autocomplete/json?input=QUERY&key=API_KEY&types=(regions). The result is returned in a JSON object, and each item has a types field consisting of an array of the types applicable to the location. The type at index 0 is the one you're interested in. It's usually something like country, or administrative_area_level_1.



Related Topics



Leave a reply



Submit