JohnTaa JohnTaa - 1 year ago 213
Javascript Question

How to limit google autocomplete results to City and Country only

I am using google autocomplete places javascript to return suggested results for my searchbox , what I need is to only show the city and the country related to the characters entered but google api will give a lot of general places results which I dont need , so how to limit the result to show only city and the country .

I am using the following Example:

<style type="text/css">
body {
font-family: sans-serif;
font-size: 14px;

<title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
<script src=";libraries=places" type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addDomListener(window, 'load', initialize);

<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on">

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download