传单在加载地图上绘制多边形

Leaflet draw polygon on load map

我有这个多边形坐标

let polygon = [
    [
        51.40545845031738,
        35.706447104954194
    ],
    [
        51.390438079833984,
        35.70568044469603
    ],
    [
        51.38288497924805,
        35.69689817401091
    ]
]

我想在加载地图时绘制多边形。也可编辑。

换句话说

如何在地图加载后在地图上显示可编辑的多边形。

我这样解决了

let polygon = [
    [
        51.40545845031738,
        35.706447104954194
    ],
    [
        51.390438079833984,
        35.70568044469603
    ],
    [
        51.38288497924805,
        35.69689817401091
    ]
];

let geojson = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Polygon",
            "coordinates": []
        }
    }]
};
let arr = [];
polygon.forEach(function (item, index) {
    arr.push([item[1], item[0]]);
});
geojson.features[0].geometry.coordinates.push(arr);

var drawnItems = new L.FeatureGroup();
var geoJsonGroup = L.geoJson(geojson).addTo(map);
addNonGroupLayers(geoJsonGroup, drawnItems);

// Would benefit from https://github.com/Leaflet/Leaflet/issues/4461
function addNonGroupLayers(sourceLayer, targetGroup) {
    if (sourceLayer instanceof L.LayerGroup) {
        sourceLayer.eachLayer(function (layer) {
            addNonGroupLayers(layer, targetGroup);
        });
    } else {
        targetGroup.addLayer(sourceLayer);
    }
}