Polygon editor

Open in CodeSandbox

A visual editor is available for polygons. A link to the editor is in the editor field of the polygon object.

<!DOCTYPE html>
<html>
    <head>
        <title>Examples. Polygon editor</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="polygonEditor.js" type="text/javascript"></script>
        <style>
            html,
            body {
                width: 100%;
                height: 100%;
                padding: 0;
                margin: 0;
                font-family: Arial;
            }

            #map {
                widht: 100%;
                height: 80%;
            }

            .header {
                padding: 5px;
            }
        </style>
    </head>
    <body>
        <p class="header">Click on the map to start creating a polygon</p>
        <div id="map"></div>
    </body>
</html>
ymaps.ready(init);

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

    // Creating a polygon with no vertices.
    var myPolygon = new ymaps.Polygon(
        [],
        {},
        {
            // The cursor in the mode for adding new vertices.
            editorDrawingCursor: "crosshair",
            // The maximum number of vertices.
            editorMaxPoints: 5,
            // Fill color.
            fillColor: "#00FF00",
            // Stroke color.
            strokeColor: "#0000FF",
            // The stroke width.
            strokeWidth: 5,
        }
    );
    // Adding the polygon to the map.
    myMap.geoObjects.add(myPolygon);

    // In the mode for adding new vertices, we change the stroke color of the polygon.
    var stateMonitor = new ymaps.Monitor(myPolygon.editor.state);
    stateMonitor.add("drawing", function (newValue) {
        myPolygon.options.set(
            "strokeColor",
            newValue ? "#FF0000" : "#0000FF"
        );
    });

    // Turning on the edit mode with the possibility of adding new vertices.
    myPolygon.editor.startDrawing();
}