Found such an example of code from the comments to the question - Smooth moving of the Yandex map on click

I substituted my values ​​in the data-goto = "КиСв" and everything stopped working.

The question is how can I configure the map switching by clicking on the link (address via data-goto ) and how to place a custom marker on the map with this same address?

I suspect that the conclusion of the marker can be done somehow

 myPlacemark2 = new ymaps.Placemark(destinations['КиСв'], { balloonContent: ... 

But something in any way I will not connect something with destinations[data-goto] .

  //ДоТдёмся Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ API ΠΈ готовности DOM. ymaps.ready(init); function init() { var result = document.getElementById('result'), // для ΠΎΡ‚Π»Π°Π΄ΠΊΠΈ // Π² этой вСрсии ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ просто элСмСнты массива (ΠΈ ΠΎΠ½ΠΈ помСняны мСстами) destinations = { 'Москва': [55.753559, 37.609218], 'Π‘Π°Π½ΠΊΡ‚-ΠŸΠ΅Ρ‚Π΅Ρ€Π±ΡƒΡ€Π³': [59.938531, 30.313497], 'Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³': [56.829748, 60.617435], 'ОдСсса': [46.466444, 30.7058] }, // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ экзСмпляра ΠΊΠ°Ρ€Ρ‚Ρ‹ ΠΈ Π΅Π³ΠΎ привязка ΠΊ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρƒ с // Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌ id ("map"). myMap = new ymaps.Map('map', { // ΠŸΡ€ΠΈ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ ΠΊΠ°Ρ€Ρ‚Ρ‹ ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ // Π΅Ρ‘ Ρ†Π΅Π½Ρ‚Ρ€ ΠΈ коэффициСнт ΠΌΠ°ΡΡˆΡ‚Π°Π±ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΡ. center: destinations['Москва'], // Москва zoom: 10 }); // всС ΠΎΠΊ result.textContent = 'map init'; // ΠΊΡƒΠ΄Π° ΡΠΊΠ°ΠΊΠ°Ρ‚ΡŒ function clickGoto() { // Π³ΠΎΡ€ΠΎΠ΄ var pos = this.getAttribute('data-goto'); // ΠΈΠ»ΠΈ this.getAttribute('title') result.textContent = pos; // ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌ myMap.panTo(destinations[pos], { flying: 1 }); return false; } // навСшиваСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ var col = document.getElementsByClassName('goto'); for (var i = 0, n = col.length; i < n; ++i) { col[i].onclick = clickGoto; result.textContent = result.textContent + ' ' + i; } } 
 <script src="http://api-maps.yandex.ru/2.1/?load=package.standard&lang=ru-RU" type="text/javascript"></script> <div id="map" style="width:400px; height:300px"></div> <a href=\"#\" class="goto" data-goto="Москва" title="Москва">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="КиСв" title="КиСв">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³" title="Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="ОдСсса" title="ОдСсса">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a> <div id="result"></div> 

So the non-click custom icon does not work at all:

 //ДоТдёмся Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ API ΠΈ готовности DOM. ymaps.ready(init); function init() { // Π² этой вСрсии ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ просто элСмСнты массива (ΠΈ ΠΎΠ½ΠΈ помСняны мСстами) destinations = { 'Москва': [55.753559, 37.609218], 'Π‘Π°Π½ΠΊΡ‚-ΠŸΠ΅Ρ‚Π΅Ρ€Π±ΡƒΡ€Π³': [59.938531, 30.313497], 'Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³': [56.829748, 60.617435], 'ОдСсса': [46.466444, 30.7058] }, // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ экзСмпляра ΠΊΠ°Ρ€Ρ‚Ρ‹ ΠΈ Π΅Π³ΠΎ привязка ΠΊ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€Ρƒ с // Π·Π°Π΄Π°Π½Π½Ρ‹ΠΌ id ("map"). myMap = new ymaps.Map('map', { center: destinations['Москва'], zoom: 10, controls: [] }); // ΠΊΡƒΠ΄Π° ΡΠΊΠ°ΠΊΠ°Ρ‚ΡŒ function clickGoto() { // Π³ΠΎΡ€ΠΎΠ΄ var pos = this.getAttribute('data-goto'); // ΠΈΠ»ΠΈ this.getAttribute('title') result.textContent = pos; // ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌ myMap.panTo(destinations[pos], { flying: 1 }); return false; } // навСшиваСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ var col = document.getElementsByClassName('goto'); for (var i = 0, n = col.length; i < n; ++i) { col[i].onclick = clickGoto; result.textContent = result.textContent + ' ' + i; } var myPlacemark = new ymaps.Placemark(destinations['Москва'], { }, { iconImageHref: 'map-icon.png', iconImageSize: [79, 96], iconImageOffset: [0, -50], }); /* ДобавляСм */ myMap.geoObjects .add(myPlacemark); } 
  <script src="http://api-maps.yandex.ru/2.1/?load=package.standard&lang=ru-RU" type="text/javascript"></script> <div id="map" style="width:400px; height:300px"></div> <a href=\"#\" class="goto" data-goto="Москва" title="Москва">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="КиСв" title="КиСв">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³" title="Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a><br> <a href=\"#\" class="goto" data-goto="ОдСсса" title="ОдСсса">ΠŸΠ΅Ρ€Π΅ΠΉΡ‚ΠΈ</a> <div id="result"></div> 

Help please solve the problem !!!

All addresses are not known in advance, they will be edited only through the data attribute and not through a script, so it makes no sense to manually register all addresses at destinations .

In general, there are links with addresses such as string, map. How to display the desired address on the map by clicking on the link with this same address?

  • In order for destinations['КиСв'] to work, you need at least that these destinations['КиСв'] exist. - br3t
  • @ br3t, all addresses are not known in advance, they will be edited only through the data attribute and not through the script. - hug
  • Then you will need to register in the links and coordinates - br3t

1 answer 1

  1. You must add Kiev with the destination array to be able to switch to it.

  2. API 2.0 is a slightly different way to add markers.

  3. To put in order the attributes href for links does not hurt.

Minimally modifying your code, it turned out like this:

 //ДоТдёмся Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ API ΠΈ готовности DOM. ymaps.ready(init); function init() { var result = document.getElementById('result'), myMap = new ymaps.Map('map', { center: [55.753559, 37.609218], // Москва zoom: 12 }); // всС ΠΎΠΊ result.textContent = 'map init'; // ΠΊΡƒΠ΄Π° ΡΠΊΠ°ΠΊΠ°Ρ‚ΡŒ function clickGoto() { // Π³ΠΎΡ€ΠΎΠ΄ var city = this.getAttribute('data-goto'); // ΠΈΠ»ΠΈ this.getAttribute('title') result.textContent = city; // ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ ΠΏΠΎ адрСсу - асинхронная функция var myGeocoder = ymaps.geocode(city); myGeocoder.then( function(res) { coords = res.geoObjects.get(0).geometry.getCoordinates(); // ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌ myMap.panTo(coords, { flying: 1 }); // добавляСм ΠΌΠ°Ρ€ΠΊΠ΅Ρ€ var placeMark = new ymaps.Placemark(coords, { balloonContent: city }); myMap.geoObjects.add(placeMark); }, function(err) { alert('Ошибка'); } ); return false; } // навСшиваСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ var col = document.getElementsByClassName('goto'); for (var i = 0, n = col.length; i < n; ++i) { col[i].onclick = clickGoto; result.textContent = result.textContent + ' ' + i; } } 
 <script src="https://api-maps.yandex.ru/2.0/?load=package.standard&lang=ru-RU" type="text/javascript"></script> <div id="map" style="width:400px; height:300px"></div> <a href="#" class="goto" data-goto="Россия, Москва, ΠšΡ€Π°ΡΠ½Π°Ρ ΠΏΠ»ΠΎΡ‰Π°Π΄ΡŒ, 1" title="Москва">Россия, Москва, ΠšΡ€Π°ΡΠ½Π°Ρ ΠΏΠ»ΠΎΡ‰Π°Π΄ΡŒ, 1</a><br> <a href="#" class="goto" data-goto="Π£ΠΊΡ€Π°ΠΈΠ½Π°, КиСв, ΡƒΠ»ΠΈΡ†Π° ΠšΡ€Π΅Ρ‰Π°Ρ‚ΠΈΠΊ, 1/2" title="КиСв">Π£ΠΊΡ€Π°ΠΈΠ½Π°, КиСв, ΡƒΠ»ΠΈΡ†Π° ΠšΡ€Π΅Ρ‰Π°Ρ‚ΠΈΠΊ, 1/2</a><br> <a href="#" class="goto" data-goto="Россия, БвСрдловская ΠΎΠ±Π»Π°ΡΡ‚ΡŒ, Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³, проспСкт Π›Π΅Π½ΠΈΠ½Π°, 1" title="Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³">Россия, БвСрдловская ΠΎΠ±Π»Π°ΡΡ‚ΡŒ, Π•ΠΊΠ°Ρ‚Π΅Ρ€ΠΈΠ½Π±ΡƒΡ€Π³, проспСкт Π›Π΅Π½ΠΈΠ½Π°, 1</a><br> <a href="#" class="goto" data-goto="Π£ΠΊΡ€Π°ΠΈΠ½Π°, ОдСсса, ΠŸΡ€ΠΈΠΌΠΎΡ€ΡΠΊΠΈΠΉ Π±ΡƒΠ»ΡŒΠ²Π°Ρ€, 1" title="ОдСсса">Π£ΠΊΡ€Π°ΠΈΠ½Π°, ОдСсса, ΠŸΡ€ΠΈΠΌΠΎΡ€ΡΠΊΠΈΠΉ Π±ΡƒΠ»ΡŒΠ²Π°Ρ€, 1</a> <div id="result"></div> 

Ideally, store information about which markers have already been created so as not to create them again, but yandex doesn’t seem to duplicate them.

  • "In general, there are links with addresses such as string, map. How to display the desired address on the map by clicking on the link with this same address?" - hug
  • one
    Addresses without coordinates? - br3t
  • 2
    @hug, fixed the snippet to addresses without coordinates. - br3t
  • thanks, what you need. Reward after 20 hours, your 100 points! - hug
  • And how to make boostrap4 in a modal window? - Never3D