OpenLayer无法在地图上添加面要素

时间:2019-02-05 12:58:33

标签: openlayers openlayers-3 openlayers-5

我正在尝试在openlayers地图上添加多边形,但未渲染该多边形。我也尝试过变换多边形的点,但在控制台中也没有错误,也没有输出。不知道我在做什么错。请指出我的方向。

Here is a fiddleopenlayers polygon demo

2 个答案:

答案 0 :(得分:3)

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

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"
});

答案 1 :(得分:0)

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

    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);