Google Auto Complete for City

Google Places: Limit Autocomplete Results to Cities Only, Hints

by Good Muyis

HTML

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

JavaScript

google.load("maps", "3.x", {
  callback: initialize,
  other_params: 'sensor=false&libraries=places&region=NG'
});

function initialize() {
  var input = document.getElementById('id_location');
  var autocomplete = new google.maps.places.Autocomplete(input, {
    types: ['(cities)'],
    componentRestrictions: {country: "NG"},
    //region: 'NG'
  });
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      console.log('no location');
      return;
    }
    console.log(place);
  });
}