Hello. There is a script with the help of which, when entering the first letters of the city name, a list of cities appears. Everything works great.

Question: How to get the country and city in separate fields input . For example, I enter Moscow , a list appears and I select Moscow, Moscow, Russia , then Russia should appear in the input name="strana" field, and input name="strana" should appear in the input name="gorod" field.

I would be very grateful for the help!

  function initialize() { var input = document.getElementById('searchTextField'); var options = { types: ['(cities)'], }; var autocomplete = new google.maps.places.Autocomplete(input, options); google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); //получаем место console.log(place); console.log(place.name); //название места console.log(place.id); //уникальный идентификатор места }); } google.maps.event.addDomListener(window, 'load', initialize); 
 <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBsqnZcch_56fMZAFOeO_eSh2nsqqLFWGY&amp;libraries=places"></script> Введите город: <input id="searchTextField" size="50" type="text" /><br /><br /> Страна: <input name="strana" size="50" type="text" /><br /> Город: <input name="gorod" size="50" type="text" /><br /> 

    2 answers 2

     function initialize() { var input = document.getElementById('searchTextField'); var options = { types: ['(cities)'], }; var autocomplete = new google.maps.places.Autocomplete(input, options); google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); //получаем место // массив адресов var address = place.address_components; // населенный пункт в нулевом элементе document.getElementById('city').value = address[0].long_name; // страна в элементе, который в подмассиве types содержит элемент со значением 'country' for (var i = address.length - 1; i > 0; i--) { if (address[i].types.indexOf("country") !== -1) { document.getElementById('country').value = address[i].long_name; break; } } console.log(address); // console.log(place); // console.log(place.name); //название места // console.log(place.id); //уникальный идентификатор места }); } google.maps.event.addDomListener(window, 'load', initialize); 
     <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBsqnZcch_56fMZAFOeO_eSh2nsqqLFWGY&amp;libraries=places"></script> Введите город: <input id="searchTextField" size="50" type="text" /><br /><br /> Страна: <input id="country" name="strana" size="50" type="text" /><br /> Город: <input id="city" name="gorod" size="50" type="text" /><br /> 

      // Added id

      <input name="strana" id="strana" size="50" type="text" /><br /> <input name="gorod" id="gorod" size="50" type="text" /><br />

      // Get the item and assign the value.

      document.getElementById("gorod").value = place.name; document.getElementById("strana").value = place.address_components[3].long_name;

      • Considered this option. City get - no problem. But with the choice of the country from the array - the problem. The number of objects in the array is not always the same ... - iKey
      • var strana = place.formatted_address.split (','); document.getElementById ("strana"). value = strana [1]; - Yaroslav Zaika
      • You can also loop through all the objects and make a comparison. We'll have to create an array and register all cuts of countries. var country = ['BY', 'UA', 'US', 'RU']; place.address_components.forEach (function (obj) {country.forEach (function (country) {if (obj.short_name == country) {console.log ('long_name', obj.long_name)}})}); - Yaroslav Zaika