Custom layout for the multiroute balloon

Open in CodeSandbox

Object layouts can be created using the templateLayoutFactory factory and text templates.

This example creates a custom multiroute balloon layout that displays information about the selected route: each type of transport shows the route, the length of the route, and the travel time. The balloon layout is set via the multirouter option.

<!DOCTYPE html>

<html>
    <head>
        <title>Examples. Custom layout for the multiroute balloon</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_custom_balloon_layout.js"
            type="text/javascript"
        ></script>
        <style>
            html,
            body,
            #map {
                width: 100%;
                height: 100%;
                padding: 0;
                margin: 0;
            }
            .my-balloon {
                display: inline-block;
                padding: 4px 10px;
                height: 75px;
                position: relative;
                bottom: 80px;
                left: -10px;
                width: 150px;
                font-size: 11px;
                line-height: 15px;
                color: #333333;
                text-align: left;
                vertical-align: middle;
                background-color: #fff0f5;
                border: 1px solid #cdb7b5;
                border-radius: 20px;
                font-family: Arial;
            }
        </style>
    </head>

    <body>
        <div id="map"></div>
    </body>
</html>
function init() {
    var myMap = new ymaps.Map("map", {
            center: [55.752625, 37.5981],
            zoom: 14,
            controls: [],
        }),
        /**
         * Creating your own layout using the layout factory.
         * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/templateLayoutFactory.xml
         */
        balloonLayout = ymaps.templateLayoutFactory.createClass(
            "<div class='my-balloon'>" +
                "<u>Route {% if properties.type == 'driving' %}" +
                "Driving<br/>" +
                "{% else %}" +
                "Public transit" +
                "{% endif %}</u><br />" +
                "Distance: " +
                "<i>{{ properties.distance.text }}</i>,<br />" +
                "Travel time: " +
                "<i>{{ properties.duration.text }} (without traffic) </i>." +
                "</div>"
        ),
        /**
         * Creating a multiroute.
         * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/multiRouter.MultiRoute.xml
         */
        multiRoute = new ymaps.multiRouter.MultiRoute(
            {
                referencePoints: [
                    "Arbatskaya metro station",
                    "Smolenskaya metro station",
                ],
                params: {
                    /**
                     * avoidTrafficJams: true,
                     * routingMode: 'masstransit'
                     */
                },
            },
            {
                /**
                 * Geo object layout.
                 * @see https://api.yandex.com/maps/doc/jsapi/2.1/ref/reference/GeoObject.xml#param-options
                 */
                balloonLayout: balloonLayout,
                // Disabling the panel mode for the balloon.
                balloonPanelMaxMapArea: 0,
            }
        );

    myMap.geoObjects.add(multiRoute);
}

ymaps.ready(init);