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
Can Es6 Template Literals Be Substituted at Runtime (Or Reused)
How to Do Outerhtml in Firefox
JavaScript Associative Array to JSON
How to Use Underscore.Js as a Template Engine
How to Dynamically Insert a <Script> Tag via Jquery After Page Load
JavaScript Drag and Drop for Touch Devices
How to Link Each User to Their Data in Firebase
Sort by Two Values Prioritizing on One of Them
Update Style of a Component Onscroll in React.Js
How to Convert Date to Timestamp
Pass Parameters in Setinterval Function
Scrolling an Iframe with JavaScript
How to Convert an Integer to Binary in JavaScript
Using Address Instead of Longitude and Latitude with Google Maps API
How to Access Local Scope Dynamically in JavaScript
$(Document).Ready(Function(){}); VS Script at the Bottom of Page