Is there any way to fix the code so that when you click on the link the route changes? Those. In the second value of the route "Новый Π³ΠΎΡ€ΠΎΠ΄" value from the data-map ( pos ) was substituted and the route was changed?

If I put onclick inside the init() card is not initialized before the click.

 $('.map__link').on('click', function(e){ e.preventDefault(); var pos = $(this).data('map'); }); $('.map__link').on('click', function(e){ e.preventDefault(); var pos = $(this).data('map'); return pos; }); function init () { // ОбъявляСм Π½Π°Π±ΠΎΡ€ ΠΎΠΏΠΎΡ€Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ, ΠΈ массив индСксов Ρ‚Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ. var referencePoints = [ "Россия, Москва", "Новый Π³ΠΎΡ€ΠΎΠ΄" ]; // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΌΡƒΠ»ΡŒΡ‚ΠΈΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ ΠΈ настраиваСм Π΅Π³ΠΎ внСшний Π²ΠΈΠ΄ с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ ΠΎΠΏΡ†ΠΈΠΉ. var multiRoute = new ymaps.multiRouter.MultiRoute({ referencePoints: referencePoints }, { // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ ΠΏΡƒΡ‚Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ. wayPointStartIconColor: "#000000", // Π—Π°Π΄Π°Π΅ΠΌ ΡΠΎΠ±ΡΡ‚Π²Π΅Π½Π½ΡƒΡŽ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ для послСднСй ΠΏΡƒΡ‚Π΅Π²ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. // wayPointFinishIconLayout: "default#image", // wayPointFinishIconImageHref: "images/sokolniki.png", wayPointFinishIconLayout: 'islands#icon', iconColor: '#0095b6', wayPointFinishIconImageSize: [30, 30], wayPointFinishIconImageOffset: [-15, -15], // ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΡΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΈΠΊΠΎΠ½ΠΊΠΈ ΠΏΡƒΡ‚Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. // wayPointVisible:false, // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ Ρ‚Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ. viaPointIconRadius: 7, viaPointIconFillColor: "#000088", viaPointActiveIconFillColor: "#fc6059", // Π’Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅Ρ‚Π°ΡΠΊΠΈΠ²Π°Ρ‚ΡŒ, ΠΏΡ€ΠΈ этом // ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅ΡΡ‚Ρ€Π°ΠΈΠ²Π°Ρ‚ΡŒΡΡ. viaPointDraggable: true, // ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΡΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΈΠΊΠΎΠ½ΠΊΠΈ Ρ‚Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. // viaPointVisible:false, // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ Ρ‚ΠΎΡ‡Π΅Ρ‡Π½Ρ‹Ρ… ΠΌΠ°Ρ€ΠΊΠ΅Ρ€ΠΎΠ² ΠΏΠΎΠ΄ ΠΏΡƒΡ‚Π΅Π²Ρ‹ΠΌΠΈ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ. pinIconFillColor: "#000088", pinActiveIconFillColor: "#fc6059", // ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΡΠΊΡ€Ρ‹Ρ‚ΡŒ Ρ‚ΠΎΡ‡Π΅Ρ‡Π½Ρ‹Π΅ ΠΌΠ°Ρ€ΠΊΠ΅Ρ€Ρ‹ ΠΏΡƒΡ‚Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ. // pinVisible:false, // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ Π»ΠΈΠ½ΠΈΠΈ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. routeStrokeWidth: 2, routeStrokeColor: "#000088", routeActiveStrokeWidth: 6, routeActiveStrokeColor: "#fc6059", // АвтоматичСски ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°Ρ‚ΡŒ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΊΠ°Ρ€Ρ‚Ρ‹ Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π±Ρ‹Π» Π²ΠΈΠ΄Π΅Π½ Ρ†Π΅Π»ΠΈΠΊΠΎΠΌ. boundsAutoApply: true }); // НастраиваСм внСшний Π²ΠΈΠ΄ Π²Ρ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ Ρ‡Π΅Ρ€Π΅Π· прямой доступ ΠΊ Π½Π΅ΠΉ. customizeSecondPoint(); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡƒ. // var removePointsButton = new ymaps.control.Button({ // data: { content: "Π£Π΄Π°Π»ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ"}, // options: { selectOnClick: true } // }); // ОбъявляСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ для ΠΊΠ½ΠΎΠΏΠΊΠΈ. // removePointsButton.events.add('select', function () { // multiRoute.model.setReferencePoints([ // referencePoints[0], // referencePoints[referencePoints.length - 1] // ], []); // }); // removePointsButton.events.add('deselect', function () { // multiRoute.model.setReferencePoints(referencePoints, viaIndexes); // // Π’.ΠΊ. вторая Ρ‚ΠΎΡ‡ΠΊΠ° Π±Ρ‹Π»Π° ΡƒΠ΄Π°Π»Π΅Π½Π°, Π½ΡƒΠΆΠ½ΠΎ Π·Π°Π½ΠΎΠ²ΠΎ Π΅Π΅ Π½Π°ΡΡ‚Ρ€ΠΎΠΈΡ‚ΡŒ. // customizeSecondPoint(); // }); // Ѐункция настройки внСшнСго Π²ΠΈΠ΄Π° Π²Ρ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. function customizeSecondPoint () { multiRoute.model.events.once("requestsuccess", function () { var yandexWayPoint = multiRoute.getWayPoints().get(1); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Π±Π°Π»ΡƒΠ½ Ρƒ ΠΌΠ΅Ρ‚ΠΊΠΈ Π²Ρ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. ymaps.geoObject.addon.balloon.get(yandexWayPoint); yandexWayPoint.options.set({ preset: "islands#icon", // iconContentLayout: ymaps.templateLayoutFactory.createClass( // '<span style="color: red;">Π―</span>ндСкс' // ), // balloonContentLayout: ymaps.templateLayoutFactory.createClass( // '{{ properties.address|raw }}' // ) }); }); } // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚Ρƒ с Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½ΠΎΠΉ Π½Π° Π½Π΅Π΅ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ. var myMap = new ymaps.Map('map', { center: [55.739625, 37.54120], zoom: 7, // controls: [removePointsButton] }, { buttonMaxWidth: 300 }); // ДобавляСм ΠΌΡƒΠ»ΡŒΡ‚ΠΈΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π½Π° ΠΊΠ°Ρ€Ρ‚Ρƒ. myMap.geoObjects.add(multiRoute); } ymaps.ready(init); 
 #map { height: 400px; width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script> <div id="map"></div> <a href="#" data-map="Москва" class="map__link active"> Π‘Ρ…Π΅ΠΌΠ° 1 </a> <a href="#" data-map="КиСв" class="map__link"> Π‘Ρ…Π΅ΠΌΠ° 2 </a> <a href="#" data-map="Π’ΠΎΡ€ΠΎΠ½Π΅ΠΆ" class="map__link"> Π‘Ρ…Π΅ΠΌΠ° 3 </a> 

  • The label "code inspection" is code-review and not "how to solve the problem." That is, "look, I have a working code, what's wrong with it"? Therefore, perhaps it is not needed here. - Duck Learns to Take Cover
  • Should only one route be shown or all three, but one is active? - Grundy
  • @Grundy, one active, by clicking on the corresponding button - HamSter

3 answers 3

The route is built immediately, so instead of substituting the value of "New City" to what is indicated in the date attribute, the route to the most suitable place is simply built.

Instead, you can move the declaration of handlers inside the init function and already inside the handlers, create routes and add / remove them from the map.

Also for the first run, you can immediately take the value from the active element.

The handler itself may look like this:

  1. if the item is already active, do nothing.
  2. remove the active class from the current active item.
  3. Add a class to the active element that was clicked.
  4. remove the added route from the map.
  5. Add a new route to the map based on the data attribute.

Example:

 function createRoute(points) { var route = new ymaps.multiRouter.MultiRoute({ referencePoints: points }, { wayPointStartIconColor: "#000000", wayPointFinishIconLayout: 'islands#icon', iconColor: '#0095b6', wayPointFinishIconImageSize: [30, 30], wayPointFinishIconImageOffset: [-15, -15], viaPointIconRadius: 7, viaPointIconFillColor: "#000088", viaPointActiveIconFillColor: "#fc6059", viaPointDraggable: true, pinIconFillColor: "#000088", pinActiveIconFillColor: "#fc6059", routeStrokeWidth: 2, routeStrokeColor: "#000088", routeActiveStrokeWidth: 6, routeActiveStrokeColor: "#fc6059", boundsAutoApply: true }); customizeSecondPoint(route); return route; } // Ѐункция настройки внСшнСго Π²ΠΈΠ΄Π° Π²Ρ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. function customizeSecondPoint(multiRoute) { multiRoute.model.events.once("requestsuccess", function() { var yandexWayPoint = multiRoute.getWayPoints().get(1); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Π±Π°Π»ΡƒΠ½ Ρƒ ΠΌΠ΅Ρ‚ΠΊΠΈ Π²Ρ‚ΠΎΡ€ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. ymaps.geoObject.addon.balloon.get(yandexWayPoint); yandexWayPoint.options.set({ preset: "islands#icon" }); }); } function init() { // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚Ρƒ с Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½ΠΎΠΉ Π½Π° Π½Π΅Π΅ ΠΊΠ½ΠΎΠΏΠΊΠΎΠΉ. myMap = new ymaps.Map('map', { center: [55.739625, 37.54120], zoom: 7, }, { buttonMaxWidth: 300 }); var activeRoute = createRoute(["Россия, Москва", $('.map__link.active').data('map')]); myMap.geoObjects.add(activeRoute) $('.map__link').on('click', function(e) { e.preventDefault(); if ($(this).hasClass('active')) return; $('.active').removeClass('active'); $(this).addClass('active'); // удаляСм Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½Ρ‹Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Ρ‹ myMap.geoObjects.removeAll(); var activeRoute = createRoute(["Россия, Москва", $(this).data('map')]); myMap.geoObjects.add(activeRoute) }); } ymaps.ready(init); 
 #map { height: 400px; width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script> <div id="map"></div> <a href="#" data-map="Минск" class="map__link active"> Π‘Ρ…Π΅ΠΌΠ° 1 </a> <a href="#" data-map="КиСв" class="map__link"> Π‘Ρ…Π΅ΠΌΠ° 2 </a> <a href="#" data-map="Π’ΠΎΡ€ΠΎΠ½Π΅ΠΆ" class="map__link"> Π‘Ρ…Π΅ΠΌΠ° 3 </a> 

In the example above, routes are created on click each time, instead they can be created once and simply select the one you need.

In addition, it was not possible to use the remove method to remove the current route, so removeAll is used instead, which removes all objects.

In the current example, there is no difference, it certainly will not be if something other than one route is added to the map.

  • As always cool! Thank you very much! True, I have already done it differently - using the buttons new ymaps.control.Button and stylized them for myself, but your answer is certainly better! ) - HamSter
  • @ElenaSemenchenko, in fact, not sure which is better, and if you yourself decided the question: add the solution as your own answer! - Grundy
  • I still think that your answer is better, the main thing is more convenient! - HamSter
  • one
    I will add a little later! - HamSter
  • one
    @Nikolay, why not? Add an answer with route editing. - Grundy

My decision at the time:

 function init () { // Π’ΠΎΡ‡ΠΊΠΈ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π° для ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° var pointA = "Россия, Москва, АщСулов ΠΏΠ΅Ρ€Π΅ΡƒΠ»ΠΎΠΊ, Π΄ 9", pointB = "ΠΊΠΎΡ‚Ρ‚Π΅Π΄ΠΆΠ½Ρ‹ΠΉ посСлок ЛСсныС ΠžΠ·Ρ‘Ρ€Π°, Россия, Московская ΠΎΠ±Π»Π°ΡΡ‚ΡŒ, ЧСховский Ρ€Π°ΠΉΠΎΠ½", pointC = 'Россия, Π’ΠΎΡ€ΠΎΠ½Π΅ΠΆ ', pointD = 'Россия, ЩСлковскоС шоссС ' /** * Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΌΡƒΠ»ΡŒΡ‚ΠΈΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚. * @see https://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/multiRouter.MultiRoute.xml */ multiRoute = new ymaps.multiRouter.MultiRoute({ referencePoints: [ pointA, pointB ], params: { // ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ Π½Π° максимальноС количСство ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ΠΎΠ², Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ΅ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ΠΈΠ·Π°Ρ‚ΠΎΡ€ΠΎΠΌ. results: 4 } }, { // АвтоматичСски ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°Ρ‚ΡŒ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΊΠ°Ρ€Ρ‚Ρ‹ Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π±Ρ‹Π» Π²ΠΈΠ΄Π΅Π½ Ρ†Π΅Π»ΠΈΠΊΠΎΠΌ. boundsAutoApply: true, viaPointIconRadius: 7, viaPointIconFillColor: "#000088", viaPointActiveIconFillColor: "#E63E92", // Π’Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠ΅Ρ€Π΅Ρ‚Π°ΡΠΊΠΈΠ²Π°Ρ‚ΡŒ, ΠΏΡ€ΠΈ этом // ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π΅ΡΡ‚Ρ€Π°ΠΈΠ²Π°Ρ‚ΡŒΡΡ. viaPointDraggable: true, // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ Ρ‚ΠΎΡ‡Π΅Ρ‡Π½Ρ‹Ρ… ΠΌΠ°Ρ€ΠΊΠ΅Ρ€ΠΎΠ² ΠΏΠΎΠ΄ ΠΏΡƒΡ‚Π΅Π²Ρ‹ΠΌΠΈ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ. pinIconFillColor: "#000088", pinActiveIconFillColor: "#E63E92", // ΠŸΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ‚ ΡΠΊΡ€Ρ‹Ρ‚ΡŒ Ρ‚ΠΎΡ‡Π΅Ρ‡Π½Ρ‹Π΅ ΠΌΠ°Ρ€ΠΊΠ΅Ρ€Ρ‹ ΠΏΡƒΡ‚Π΅Π²Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ. // pinVisible:false, // Π’Π½Π΅ΡˆΠ½ΠΈΠΉ Π²ΠΈΠ΄ Π»ΠΈΠ½ΠΈΠΈ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. routeStrokeWidth: 2, routeStrokeColor: "#000088", routeActiveStrokeWidth: 6, routeActiveStrokeColor: "#E63E92", // АвтоматичСски ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°Ρ‚ΡŒ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΊΠ°Ρ€Ρ‚Ρ‹ Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π±Ρ‹Π» Π²ΠΈΠ΄Π΅Π½ Ρ†Π΅Π»ΠΈΠΊΠΎΠΌ. boundsAutoApply: true }); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡƒ. var changePointsButton3 = new ymaps.control.Button({ data: {content: " "}, options: { selectOnClick: true, layout: ymaps.templateLayoutFactory.createClass( // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнится css-ΡΡ‚ΠΈΠ»ΡŒ 'myButton' // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнятся css-стили 'myButton' ΠΈ 'myButtonSelected'. "<div class='my-YButton my-YButton-3 {% if state.selected %}myButtonSelected{% endif %}' title='{{ data.title }}'>" + "{{ data.content }} ΠœΠ°Ρ€ΡˆΡ€ΡƒΡ‚ <span>" + "β„– 1</span></div>" ) } }); var changePointsButton1 = new ymaps.control.Button({ data: {content: " "}, options: { selectOnClick: true, layout: ymaps.templateLayoutFactory.createClass( // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнится css-ΡΡ‚ΠΈΠ»ΡŒ 'myButton' // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнятся css-стили 'myButton' ΠΈ 'myButtonSelected'. "<div class='my-YButton my-YButton-1 {% if state.selected %}myButtonSelected{% endif %}' title='{{ data.title }}'>" + "{{ data.content }} ΠœΠ°Ρ€ΡˆΡ€ΡƒΡ‚ <span>" + "β„– 2</span></div>" ) } }); var changePointsButton2 = new ymaps.control.Button({ data: {content: " "}, options: { selectOnClick: true, layout: ymaps.templateLayoutFactory.createClass( // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнится css-ΡΡ‚ΠΈΠ»ΡŒ 'myButton' // Если ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π°, ΠΊ Π½Π΅ΠΉ примСнятся css-стили 'myButton' ΠΈ 'myButtonSelected'. "<div class='my-YButton my-YButton-2 {% if state.selected %}myButtonSelected{% endif %}' title='{{ data.title }}'>" + "{{ data.content }} ΠœΠ°Ρ€ΡˆΡ€ΡƒΡ‚ <span>" + "β„– 3</span></div>" ) } }); // ОбъявляСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΈ для ΠΊΠ½ΠΎΠΏΠΊΠΈ. // ΠŸΠ΅Ρ€Π²Π°Ρ ΠΊΠ½ΠΎΠΏΠΊΠ° ΠΊΠ°Ρ€Ρ‚Ρ‹ changePointsButton1.events.add('select', function () { multiRoute.model.setReferencePoints([pointA, pointB]); }); changePointsButton1.events.add('deselect', function () { multiRoute.model.setReferencePoints([pointA, pointB]); }); // Вторая ΠΊΠ½ΠΎΠΏΠΊΠ° ΠΊΠ°Ρ€Ρ‚Ρ‹ changePointsButton2.events.add('select', function () { var referencePoints = multiRoute.model.getReferencePoints(); referencePoints.splice(1, 0, 'Россия, ЩСлковскоС шоссС '); /** * ДобавляСм Ρ‚Ρ€Π°Π½Π·ΠΈΡ‚Π½ΡƒΡŽ Ρ‚ΠΎΡ‡ΠΊΡƒ Π² модСль ΠΌΡƒΠ»ΡŒΡ‚ΠΈΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. * ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Ρ‚Ρ€Π°Π½Π·ΠΈΡ‚Π½Ρ‹Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΌΠΎΠ³ΡƒΡ‚ находится Ρ‚ΠΎΠ»ΡŒΠΊΠΎ * ΠΌΠ΅ΠΆΠ΄Ρƒ двумя ΠΏΡƒΡ‚Π΅Π²Ρ‹ΠΌΠΈ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ, Ρ‚.Π΅. Π½Π΅ ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΡ€Π°ΠΉΠ½ΠΈΠΌΠΈ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ ΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚Π°. * @see https://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/multiRouter.MultiRouteModel.xml#setReferencePoints */ multiRoute.model.setReferencePoints(referencePoints, [1]); }); changePointsButton2.events.add('deselect', function () { var referencePoints = multiRoute.model.getReferencePoints(); referencePoints.splice(1, 1); multiRoute.model.setReferencePoints(referencePoints, []); }); changePointsButton3.events.add('select', function () { multiRoute.model.setReferencePoints([pointA, pointC]); }); changePointsButton3.events.add('deselect', function () { multiRoute.model.setReferencePoints([pointA, pointB]); }); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚Ρƒ с Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½Ρ‹ΠΌΠΈ Π½Π° Π½Π΅Π΅ ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ. var myMap = new ymaps.Map('map', { center: [55.750625, 37.626], zoom: 7, controls: [changePointsButton1, changePointsButton2, changePointsButton3] }, { buttonMaxWidth: 300 }); // ДобавляСм ΠΌΡƒΠ»ΡŒΡ‚ΠΈΠΌΠ°Ρ€ΡˆΡ€ΡƒΡ‚ Π½Π° ΠΊΠ°Ρ€Ρ‚Ρƒ. myMap.geoObjects.add(multiRoute); } ymaps.ready(init); 
 #map { height: 498px; width: 100%; } .my-YButton { font-size: 1.375rem; line-height: 1.5rem; color: #0f3223; display: block; text-align: center !important; background: rgba(255,255,255,.8); width: 295px; border: 1px solid transparent; border-radius: 10px; margin-bottom: 20px; padding: 7px 0; position: relative; cursor: pointer; } .my-YButton { position: absolute; left: 100px; z-index: 9999; } .my-YButton-1 { top: 75px; } .my-YButton-2 { top: 159px; } .my-YButton-3 { top: 243px; } .my-YButton.myButtonSelected { border: 1px solid #0f3223; } .my-YButton span { font-weight: bold; display: block; text-align: center !important; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="http://api-maps.yandex.ru/2.1/?lang=ru_RU"></script> <div id="map"></div> 

Cons: it is difficult to stylize, switching routes works something like a checkbox, ...

    This function is repeatedly mentioned in the documentation and examples. Please read the documentation.

    multiRoute.model.setReferencePoints([point1, point2]));

    The code changes the points to those specified inside the array. To edit points with a mouse, that is, methods:

    multiRoute.editor.start(); and multiRoute.editor.stop();

    The first allows you to drag points on the map with the mouse, and the second method closes the editing mode. By the button, for example.

    • one
      Add a description of what the code is, what it does, why it should be used. It is advisable to add an example of use based on the code from the question. - Grundy
    • one
      And also look at the next answer of the author of the question :) - Grundy
    • @Grundy though I merged (But I clearly don’t understand what creation is in the right answer .. - Nikolay
    • @Grundy and why on the correct answer minus, and on the answer, which in no case can not be used with the capabilities of api, hangs 1 plus - Nikolay
    • one
      The accepted answer is convenient and works everywhere, and most importantly was given in a timely manner! Your answer can hardly be called an answer at all, some cues of the hint and no more, you can not see the working code. Give a full answer and you will get a plus, or even much more! =) PS: minus not from me! - HamSter