Openlayers 无法在地图上添加多边形特征

Openlayers not able to add polygon feature on the map

我正在尝试在 openlayers 地图上添加一个多边形,但它没有被渲染。我也尝试过转换多边形的点,但控制台仍然没有错误,也没有输出。不知道我的做法有什么问题。请指点方向

Here is a fiddle : openlayers polygon demo

多边形的坐标需要一组额外的[],并且转换整个几何体而不是单个坐标更容易

var data=[[119.76574, 24.21667], [118.03333, 24.21667], [118.03333, 25.78333], [120.55, 25.78333], [120.55, 24.21667], [119.85674, 24.21667], [119.76574, 24.21667]];

var polygon = new ol.Feature({
  type: 'Polygon',
  geometry: new ol.geom.Polygon([data]).transform('EPSG:4326','EPSG:3857'),
  desc: "Description" + "<br>" + "This is on of the ENC"
});

您在转换和几何创建方面遇到了问题。检查此更正后的代码:

    var data = [[119.76574, 24.21667], [118.03333, 24.21667], [118.03333, 25.78333], [120.55, 25.78333], [120.55, 24.21667], [119.85674, 24.21667], [119.76574, 24.21667]];

    data.forEach(function (item) {
        var newItem = ol.proj.transform(item, 'EPSG:4326', 'EPSG:3857');
        item[0] = newItem[0];
        item[1] = newItem[1];
    });

    var polygon = new ol.Feature({
        type: 'Polygon',
        geometry: new ol.geom.Polygon([data]),
        desc: "Description" + "<br>" + "This is on of the ENC"
    });
    polygon.setStyle(polygonOptions);
    drawingSource.addFeature(polygon);