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

# The intersection of a route with a polygon using the example of the Moscow Ring Road

<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/route_inside_polygon/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/t4xq5z?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/Polygon">Polygon</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/route">route</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/geoQuery">geoQuery</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/GeoQueryResult">GeoQueryResult</a>
</div>
<p>
    It is often necessary to draw a route between specified points, and then analyze in what areas or cities the segments of this route fall on.
 In this example, prepared data is used for creating a polygon defining the MKAD. After the route is received, a check is performed for whether its segments fall within this polygon using the method <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/GeoQueryResult#searchInside">searchInside</a>.
</p>
<p>
    You can use the <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/GeoQueryResult">GeoQueryResult</a> object to set options or data for an entire group of geo objects at once. Here you can see how route segments that are inside or outside of the ring road use different colors.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>

    <html>
        <head>
            <title>
                Examples. The intersection of a route with a polygon using the
                example of the Moscow Ring Road.
            </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>
            <p>
                The route inside the ring road is marked in red, and outside is
                blue
            </p>
            <div id="map"></div>
        </body>
    </html>
    ```

-   route_inside_polygon.js

    ```js
    ymaps.ready(init);

    function init() {
        var myMap = new ymaps.Map(
                "map",
                {
                    center: [55.73, 37.75],
                    zoom: 9,
                },
                {
                    searchControlProvider: "yandex#search",
                }
            ),
            moscowPolygon;

        function onPolygonLoad(json) {
            moscowPolygon = new ymaps.Polygon(json.coordinates);
            // If we do not want the outline to be visible, we set the appropriate option.
            moscowPolygon.options.set("visible", false);
            /**
             * To correctly carried out geometric operations on the projected polygon,
             * you must add it to the map.
             */
            myMap.geoObjects.add(moscowPolygon);

            ymaps
                .route([
                    [55.654884, 37.527034],
                    [55.767305, 37.9761],
                ])
                .then(function (res) {
                    // Combining all the route segments in a selection.
                    var pathsObjects = ymaps.geoQuery(res.getPaths()),
                        edges = [];

                    // Iterating all the segments and dividing them into sections.
                    pathsObjects.each(function (path) {
                        var coordinates = path.geometry.getCoordinates();
                        for (var i = 1, l = coordinates.length; i < l; i++) {
                            edges.push({
                                type: "LineString",
                                coordinates: [
                                    coordinates[i],
                                    coordinates[i - 1],
                                ],
                            });
                        }
                    });

                    /**
                     * Creating a new selection containing the following:
                     * - sections that define the route;
                     * - the start and end points;
                     * - intermediate points.
                     */
                    var routeObjects = ymaps
                            .geoQuery(edges)
                            .add(res.getWayPoints())
                            .add(res.getViaPoints())
                            .setOptions("strokeWidth", 3)
                            .addToMap(myMap),
                        // Finding all the objects that fall inside the ring road.
                        objectsInMoscow =
                            routeObjects.searchInside(moscowPolygon),
                        // Finding the objects that intersect with the ring road.
                        boundaryObjects =
                            routeObjects.searchIntersect(moscowPolygon);
                    // Using different colors for objects inside, outside and intersecting the ring road.
                    boundaryObjects.setOptions({
                        strokeColor: "#06ff00",
                        preset: "islands#greenIcon",
                    });
                    objectsInMoscow.setOptions({
                        strokeColor: "#ff0005",
                        preset: "islands#redIcon",
                    });
                    /**
                     * Getting objects outside the Moscow ring road
                     * by subtracting the obtained samples from the source sample.
                     */
                    routeObjects
                        .remove(objectsInMoscow)
                        .remove(boundaryObjects)
                        .setOptions({
                            strokeColor: "#0010ff",
                            preset: "islands#blueIcon",
                        });
                });
        }

        $.ajax({
            url: "moscow.json",
            dataType: "json",
            success: onPolygonLoad,
        });
    }
    ```

{% endlist %}
