S
S
SergeyTSA2016-09-27 21:20:31
Yandex maps
SergeyTSA, 2016-09-27 21:20:31

How to center and scale the map in Yandex maps after laying the route so that the entire route fits in the visible area?

Hello!
You have the following code on the page, which creates a map centered on City1, then routes between City1 and City2, and you want to. so that after the route is laid, fit the entire route in the visible area of ​​the map.

ymaps.ready(init);

function init() {
  ymaps.geocode('Москва', { results: 1 }).then(function(res){
    var city = res.geoObjects.get(0);
    
    var myMap = new ymaps.Map("route-map", {
          center: city.geometry.getCoordinates(),
          zoom: 4
     });
    
    ymaps.route(['Москва','Самара']).then(function (route) {
      myMap.geoObjects.add(route);
      var points = route.getWayPoints(),
        lastPoint = points.getLength() - 1;
      points.options.set('preset', 'twirl#redStretchyIcon');
      points.get(0).properties.set('iconContent', 'A');
      points.get(lastPoint).properties.set('iconContent', 'B');

    }, function (error) {
      alert('Возникла ошибка: ' + error.message);
    });
  });
}

Tried calling the following functions:
myMap.setBounds(route.getBounds(), { margin: 50, checkZoomRange: true });
myMap.setZoom(myMap.getZoom());

But nothing has changed, the center of the map remains at the City1 point, and if the route is long enough, then the end point of City2 is outside the visibility area.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kalibrov, 2016-09-28
@SergeyTSA

An example of where it works https://tech.yandex.ru/maps/jsbox/2.1/multiroute_v...
Setting responsible for this boundsAutoApply

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question