M
M
Max2015-09-27 23:54:22
Yandex maps
Max, 2015-09-27 23:54:22

How to set map scale automatically?

It is necessary that the zoom of the map be set as close as possible based on the marks, so that they all fit on the map and are visible.
I do it like this:

var barMap = new ymaps.Map('y-map', {
    center: [55.72504493, 37.64696100],
    zoom: 10,
    behaviors: ['default', 'scrollZoom']
}), coords = [], barList = [someJsonData];
var collection = new ymaps.GeoObjectCollection();
 
for (var i in barList) {
    coords = barList[i].coords.split(',');
    collection.add(new ymaps.Placemark([coords[0], coords[1]], {
        balloonContentHeader: '<a href="/bars/' + barList[i].link + '" target="_blank" title="Откроется в новом окне">' + barList[i].name + '</a>',
        balloonContent: barList[i].address
    }));
}
 
barMap.geoObjects.add(collection);
barMap.setBounds(collection.getBounds());

As a result, I see this: joxi.ru/L21jx64TVOKYAX

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2015-09-28
@kopcapuk

Understood what the problem is.
Initially, the map is in a hidden div.

F
forgotten, 2015-09-28
@forgotten

Judging by the code, your coordinates are given by strings, not numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question