Building a multiroute on public transport

Open in CodeSandbox

In order to build a route on public transport, pass the routingMode: 'masstransit' field in the routing parameters. By default, this field takes the 'auto' value when creating a multiroute.

For more information about the multirouter parameters and options, see the example Building a driving multiroute.

<!DOCTYPE html>

<html>
    <head>
        <title>Examples. Route on public transport</title>
        <meta
            http-equiv="Content-Type"
            content="text/html; charset=UTF-8"
        />
        <!--
        Set your own API-key. Testing key is not valid for other web-sites and services.
        Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
    -->
        <script
            src="https://api-maps.yandex.ru/2.1/?lang=en_RU&amp;apikey=<your API-key>"
            type="text/javascript"
        ></script>
        <script
            src="multiroute_masstransit.js"
            type="text/javascript"
        ></script>
        <style>
            html,
            body,
            #map {
                width: 100%;
                height: 100%;
                padding: 0;
                margin: 0;
            }
        </style>
    </head>

    <body>
        <div id="map"></div>
    </body>
</html>
function init() {
    /**
     * Creating a multiroute.
     * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/multiRouter.MultiRoute.xml
     */
    var multiRoute = new ymaps.multiRouter.MultiRoute(
        {
            referencePoints: [[55.749, 37.524], "Moscow, Uspensky Lane, 7"],
            params: {
                routingMode: "masstransit",
            },
        },
        {
            // Automatically set the map boundaries so the entire route is visible.
            boundsAutoApply: true,
        }
    );

    // Creating a button.
    var changeLayoutButton = new ymaps.control.Button({
        data: { content: "Show time for walking segments" },
        options: { selectOnClick: true },
    });

    // Declaring handlers for the button.
    changeLayoutButton.events.add("select", function () {
        multiRoute.options.set(
            // routeMarkerIconContentlayout - To show the time for all segments.
            "routeWalkMarkerIconContentLayout",
            ymaps.templateLayoutFactory.createClass(
                "{{ properties.duration.text }}"
            )
        );
    });

    changeLayoutButton.events.add("deselect", function () {
        multiRoute.options.unset("routeWalkMarkerIconContentLayout");
    });

    // Creating the map with the button added to it.
    var myMap = new ymaps.Map(
        "map",
        {
            center: [55.739625, 37.5412],
            zoom: 12,
            controls: [changeLayoutButton],
        },
        {
            buttonMaxWidth: 300,
        }
    );

    // Adding a multiroute to the map.
    myMap.geoObjects.add(multiRoute);
}

ymaps.ready(init);