Hello! There is a script from the Yandex.Maps sandbox, this one

And there is an input in which you need to display the name of the city (and even better the city and address) by clicking on the map:

<input type="text" name="address" id="address"> 

Question: how to make the conclusion of the address in the input when clicking on the map.

  • What exactly is the problem - in obtaining the address or in the output of the received address in the right field? - Enikeyschik
  • In the output. It is necessary to output the result in input - Devilz
  • Then correct the title. It does not coincide with the text of the question and in general is not about what the problem is. - Enikeyschik 2:58 pm

1 answer 1

 ymaps.ready(init); function init() { var myInput = document.getElementById("address"), myPlacemark, myMap = new ymaps.Map('map', { center: [55.753994, 37.622093], zoom: 9 }, { searchControlProvider: 'yandex#search' }); // Π‘Π»ΡƒΡˆΠ°Π΅ΠΌ ΠΊΠ»ΠΈΠΊ Π½Π° ΠΊΠ°Ρ€Ρ‚Π΅. myMap.events.add('click', function (e) { var coords = e.get('coords'); // Если ΠΌΠ΅Ρ‚ΠΊΠ° ΡƒΠΆΠ΅ создана – просто ΠΏΠ΅Ρ€Π΅Π΄Π²ΠΈΠ³Π°Π΅ΠΌ Π΅Π΅. if (myPlacemark) { myPlacemark.geometry.setCoordinates(coords); } // Если Π½Π΅Ρ‚ – создаСм. else { myPlacemark = createPlacemark(coords); myMap.geoObjects.add(myPlacemark); // Π‘Π»ΡƒΡˆΠ°Π΅ΠΌ событиС окончания пСрСтаскивания Π½Π° ΠΌΠ΅Ρ‚ΠΊΠ΅. myPlacemark.events.add('dragend', function () { getAddress(myPlacemark.geometry.getCoordinates()); }); } getAddress(coords); }); // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ‚ΠΊΠΈ. function createPlacemark(coords) { return new ymaps.Placemark(coords, { iconCaption: 'поиск...' }, { preset: 'islands#violetDotIconWithCaption', draggable: true }); } // ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅ΠΌ адрСс ΠΏΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌ (ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎΠ΅ Π³Π΅ΠΎΠΊΠΎΠ΄ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅). function getAddress(coords) { myPlacemark.properties.set('iconCaption', 'поиск...'); ymaps.geocode(coords).then(function (res) { var firstGeoObject = res.geoObjects.get(0), address = firstGeoObject.getAddressLine(); myPlacemark.properties .set({ // Π€ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ строку с Π΄Π°Π½Π½Ρ‹ΠΌΠΈ ΠΎΠ± ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π΅. iconCaption: [ // НазваниС насСлСнного ΠΏΡƒΠ½ΠΊΡ‚Π° ΠΈΠ»ΠΈ Π²Ρ‹ΡˆΠ΅ΡΡ‚ΠΎΡΡ‰Π΅Π΅ административно-Ρ‚Π΅Ρ€Ρ€ΠΈΡ‚ΠΎΡ€ΠΈΠ°Π»ΡŒΠ½ΠΎΠ΅ ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅. firstGeoObject.getLocalities().length ? firstGeoObject.getLocalities() : firstGeoObject.getAdministrativeAreas(), // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΡƒΡ‚ΡŒ Π΄ΠΎ Ρ‚ΠΎΠΏΠΎΠ½ΠΈΠΌΠ°, Ссли ΠΌΠ΅Ρ‚ΠΎΠ΄ Π²Π΅Ρ€Π½ΡƒΠ» null, Π·Π°ΠΏΡ€Π°ΡˆΠΈΠ²Π°Π΅ΠΌ Π½Π°ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½ΠΈΠ΅ здания. firstGeoObject.getThoroughfare() || firstGeoObject.getPremise() ].filter(Boolean).join(', '), // Π’ качСствС ΠΊΠΎΠ½Ρ‚Π΅Π½Ρ‚Π° Π±Π°Π»ΡƒΠ½Π° Π·Π°Π΄Π°Π΅ΠΌ строку с адрСсом ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°. balloonContent: address }); myInput.value = address; }); } } 
 html, body { width: 100%; height: 95%; margin: 0; padding: 0; font-family: Arial; font-size: 14px; } #map { width: 100%; height: 95%; } #address { width: 80%; } .header { padding: 5px; } 
 <script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script> <p class="header">ΠšΠ»ΠΈΠΊΠ½ΠΈΡ‚Π΅ ΠΏΠΎ ΠΊΠ°Ρ€Ρ‚Π΅, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡƒΠ·Π½Π°Ρ‚ΡŒ адрСс</p> <input type="text" name="address" id="address"> <div id="map"></div> 

  • Andrew, thank you very much! Problem solved. - Devilz 3:12 pm
  • Well, I look at the cool "JS experts on StackOverflow" the opinion is different))), but religion itself probably does not allow the answer)))) - Andrey ProjectSoft