Answer the question
In order to leave comments, you need to log in
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);
});
});
}
myMap.setBounds(route.getBounds(), { margin: 50, checkZoomRange: true });
myMap.setZoom(myMap.getZoom());
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question