---
metadata:
  - name: generator
    content: Diplodoc Platform v5.39.1
alternate:
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/en/v2-1/examples/cases/router.md
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/ru/v2-1/examples/cases/router.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com.tr/dev/jsapi-v2-1/doc/en/llms.txt

# Building a route

<iframe id="LIVE-EXAMPLE" style="width:100%;height: 400px;border-radius: 8px;border: 1px solid rgba(92, 94, 102, 0.14);" frameBorder="0" src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-v2-1/examples/2/out/s3-cases/en/router/index.html" allow="fullscreen"></iframe>

<a target="_blank" rel="noopener noreferrer" style="display: inline-block;margin-top: 10px;cursor: pointer;border-radius: 4px;padding: 9px 12px;background: #151515;color: white;text-decoration: none;font-weight: 500" href="https://codesandbox.io/p/sandbox/8673kv?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ready">ready</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.GeoObjects">map.GeoObjects</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/route">route</a>
</div>
<p>
	For building a <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/dg/concepts/router">route</a>, use the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/route">route</a> function. Pass this function an array of points that the route should pass through, along with additional options for building the route, if needed.
</p>
<p>
	The route is calculated asynchronously (i.e. while executing the 'route' function, data is exchanged with the server). The result is passed to the handler function as a <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/GeoObjectCollection">GeoObjectCollection</a> collection, which can be placed on the map.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>

    <html>
        <head>
            <title>Examples. Route planning</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/
        -->
            
            
            
            
        </head>

        <body>
            <div id="map"></div>
            <div id="list"></div>
        </body>
    </html>
    ```

-   router.js

    ```js
    ymaps.ready(init);

    function init() {
        var myMap = new ymaps.Map(
            "map",
            {
                center: [55.745508, 37.435225],
                zoom: 13,
            },
            {
                searchControlProvider: "yandex#search",
            }
        );

        /**
         * Adding driving directions to the map
         * from Krylatsky Hills street to the metro station "Kuntsevskaya"
         * via the station "Molodezhnaya" and then to the metro station "Pionerskaya".
         * Route points can be set in 3 ways:
         * as a string, as an object, or as an array of geocoordinates.
         */
        ymaps
            .route([
                "Moscow, Krilatskiye Holmi street",
                {
                    point: "Moscow, metro Molodezhnaya",
                    /**
                     * "Molodezhnaya" metro station - a throughpoint
                     * (passing through this point, but not stopping at it).
                     */
                    type: "viaPoint",
                },
                [55.731272, 37.447198],
                "Moscow, metro Pionerskaya",
            ])
            .then(
                function (route) {
                    myMap.geoObjects.add(route);
                    /**
                     * Setting the contents of the icons for the start and end points of the route.
                     * Using the getWayPoints() method to get an array of waypoints.
                     * The getViaPoints method can be used for getting the route's through points.
                     */
                    var points = route.getWayPoints(),
                        lastPoint = points.getLength() - 1;
                    /**
                     * Setting the placemark style - icons will be red,
                     * and their images will stretch to fit the content.
                     */
                    points.options.set("preset", "islands#redStretchyIcon");
                    // Setting the placemark content in the start and end points.
                    points
                        .get(0)
                        .properties.set("iconContent", "Starting point");
                    points
                        .get(lastPoint)
                        .properties.set("iconContent", "Destination point");

                    /**
                     * Analyzing the route by segments.
                     * A segment is a section of the route up
                     * to the next change of direction.
                     * In order to obtain the route segments, you first need to obtain
                     * each path of the route separately.
                     * The entire route is divided into two paths:
                     * 1) from Krylatsky Hills street to the station "Kuntsevskaya";
                     * 2) from "Kuntsevskaya" station to "Pionerskaya".
                     */

                    var moveList = "Let's go,</br>",
                        way,
                        segments;
                    // Getting an array of paths.
                    for (var i = 0; i < route.getPaths().getLength(); i++) {
                        way = route.getPaths().get(i);
                        segments = way.getSegments();
                        for (var j = 0; j < segments.length; j++) {
                            var street = segments[j].getStreet();
                            moveList +=
                                "Going " +
                                segments[j].getHumanAction() +
                                (street ? " to " + street : "") +
                                ", passing through " +
                                segments[j].getLength() +
                                " m.,";
                            moveList += "</br>";
                        }
                    }
                    moveList += "Stopping.";
                    // Printing itinerary.
                    $("#list").append(moveList);
                },
                function (error) {
                    alert("An error occurred: " + error.message);
                }
            );
    }
    ```

{% endlist %}
