I had such a situation : when a page loads, a map with a certain number of placemarks is loaded, and I want to click on a special button (on the page, but not on the map) to display a few more placemarks.

Code (if necessary):

function ymapsShow(markType) { if (markType == true) { var myMap = new ymaps.Map('map', { center: [59.930480, 30.327198], zoom: 8, controls: [] }), // Бамая обычная синяя ΠΌΠ΅Ρ‚ΠΊΠ° myPlacemark = new ymaps.Placemark([parseFloat(document.getElementById("x").innerHTML), parseFloat(document.getElementById("y").innerHTML)], { hintContent: 'Москва!', balloonContent: 'Π‘Ρ‚ΠΎΠ»ΠΈΡ†Π° России' }); // ΠšΠΎΠ½Π΅Ρ† самой ΠΎΠ±Ρ‹Ρ‡Π½ΠΎΠΉ ΠΌΠ΅Ρ‚ΠΊΠΈ } else { var myMap = new ymaps.Map('map', { center: [59.930480, 30.327198], zoom: 8, controls: [] }), myPlacemark = new ymaps.Placemark( [parseFloat(document.getElementById("x").innerHTML), parseFloat(document.getElementById("y").innerHTML)], { hintContent: 'БобствСнный Π·Π½Π°Ρ‡ΠΎΠΊ ΠΌΠ΅Ρ‚ΠΊΠΈ', balloonContent: 'Π­Ρ‚ΠΎ красивая ΠΌΠ΅Ρ‚ΠΊΠ°' }, { // ΠžΠΏΡ†ΠΈΠΈ. // НСобходимо ΡƒΠΊΠ°Π·Π°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹ΠΉ Ρ‚ΠΈΠΏ ΠΌΠ°ΠΊΠ΅Ρ‚Π°. iconLayout: 'default#image', // Π‘Π²ΠΎΡ‘ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΌΠ΅Ρ‚ΠΊΠΈ. iconImageHref: "<%= asset_path('icons/blocks_3.svg') %>", // Π Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΌΠ΅Ρ‚ΠΊΠΈ. iconImageSize: [30, 30], // Π‘ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Π»Π΅Π²ΠΎΠ³ΠΎ Π²Π΅Ρ€Ρ…Π½Π΅Π³ΠΎ ΡƒΠ³Π»Π° ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ // Π΅Ρ‘ "Π½ΠΎΠΆΠΊΠΈ" (Ρ‚ΠΎΡ‡ΠΊΠΈ привязки). iconImageOffset: [-5, -30] }); } myMap.geoObjects.add(myPlacemark); }; 

The question is, can this be done on an already loaded map, or is it necessary to first remove it and add a new one along with a new set of placemarks?

    1 answer 1

    You can add tags to the map at any time without creating it again.

    • Thank! May I ask you to somewhat expand your answer, since I can not find a way to get an existing map into a variable. There is only var myMap = new ymaps (), which creates a new map every time. - Alexander Gorg
    • tags are always added after the map is created, for example, here: tech.yandex.ru/maps/jsbox/2.1/placemark - Reni
    • Let me disagree - in this example, they are added along with the creation of the map. I’m wondering if it is possible to get access to the map and its geo-objects again after the document (web page) is fully loaded? - Alexander Gorg
    • This is for you in the javascript tutorial on the topic about global variables) of course, you can, you just need to create a map variable outside the init function and, when adding labels, check that the map exists - Reni
    • Now it happened, thanks! - Alexander Gorg