S
S
Sergey Zhukov2015-10-15 18:43:10
Yandex maps
Sergey Zhukov, 2015-10-15 18:43:10

How to arrange dynamically Multiple addresses on the map?

All the best!
Is there a code example of how you can arrange several addresses on the map, having only a few addresses on hand?
That is, I have an AJAX request that sends me a list of several addresses in JSON format (I can send it in any format):

["г.Томск, Учебная улица 15","г.Томск, Карповский переулок 15","г.Томск, проспект Кирова 48"]

How can I place/redraw these addresses on the map?
Do I have to first run somewhere for the coordinates of these addresses, or can I just shove them into some method?
Has anyone done something similar? There are examples? Thank you!
So far, I have only come to this: I
created a map with points
ymaps.ready(init);
  var myMap;
  function init(){
    myMap = new ymaps.Map("map", {
      center: [56.49540919, 84.95061710],
      zoom: 12,
      controls: ['zoomControl']
    });

    var coords = [
        [56.458592, 84.947361],
        [56.49844, 84.950236]
      ];

    for (var i = 0; i < coords.length; i++) {
      myMap.geoObjects.add(new ymaps.Placemark(coords[i]));
    }
    myMap.setBounds(myMap.geoObjects.getBounds());
  }

Update:
$("body").on("click", '#reload', function() {
    var newCoords = [
        [56.510298, 85.029584],
        [56.475678, 85.008743]
      ]
    myMap.geoObjects.removeAll();

    for (var i = 0; i < newCoords.length; i++) {
      myMap.geoObjects.add(new ymaps.Placemark(newCoords[i]));
    }
    myMap.setBounds(myMap.geoObjects.getBounds());
  });

Those. just set the coordinates ;(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forgotten, 2015-10-15
@forgotten

var q = ymaps.geoQuery();
data.forEach(function (address) {
q.add(ymaps.geocode(address));
});
q.addToMap(map);
https://tech.yandex.ru/maps/doc/jsapi/2.1/dg/conce...
But in general, geocoding arrays of addresses on the client is a bad practice, read habrahabr.ru/company/yandex/blog/263863

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question