1

我有这个多边形坐标

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

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

换句话说

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

4

1 回答 1

1

这以这种方式为我解决了

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);
    }
}
于 2019-06-26T10:20:19.920 回答