M
M
mr_drinkens892014-09-11 16:32:19
Yandex
mr_drinkens89, 2014-09-11 16:32:19

How to dynamically add balloons to a Yandex map?

Tell me who solved this problem and who knows the way out of it.
There is a Yandex map (2.1) and a filter. When the filter is running, the coordinates of the object come from the server. How to dynamically add these elements as balloons to the created map?
At the initial load, everything is clear, an Ajax request passes in the init function, and in the loop we display balloons on the map.

myMap = new ymaps.Map('map', {center: [59.918154,30.305578], zoom: 11});
      myMap.controls.remove('trafficControl').remove('searchControl').remove('typeSelector');
      // создадим массив геообъектов
      $.ajax({
        url:'/ajax/maps',
        success:function(data){
          for(var i = 0;i<=data.length;i++){
            var mapX = data[i][0];
            var mapY = data[i][1];
            var dop = data[i][2];
            var address = data[i][3];
            var pm  = new ymaps.Placemark([mapY, mapX], {
              iconContent: "",
              hintContent:address,
           					balloonContent: dop
        				});
            myMap.geoObjects.add(pm);
          }

        }
      });

How to make dynamic output?
thank.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gasoid, 2014-09-11
@Gasoid

in your example this is done,
only make myMap a global variable
i.e. at first

var myMap;

ymaps.ready(init);

function init(){
   ....
  $.ajax({...});
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question