OpenLayers 3.13v:ol.format.GeoJSON() 问题

OpenLayers 3.13v: issue with ol.format.GeoJSON()

在 OpenLayers 3.13v 中,我使用 ol-debug.js 获得 Uncaught AssertionError: Assertion failed: format must be set when url is set,而 Uncaught TypeError: Cannot read property 'V' of undefined 使用 ol.js

我通过替换 ol.source.GeoJSON in this example

使用以下代码
  var vectorEuropa = new ol.layer.Vector({
    id: 'europa',
    source: new ol.source.Vector({
      format: ol.format.GeoJSON(),
      projection: 'EPSG:3857',
      url: '../assets/data/nutsv9_lea.geojson'
    }),
    style: defaultEuropa
  });

此外,如果我尝试创建一个像 in this example

这样的空层,我也会遇到同样的问题
  var bbox = new ol.layer.Vector({
     source: new ol.source.Vector({
         format: ol.format.GeoJSON()
     })
  });

您必须将实例传递给源的 format 选项:

var vectorEuropa = new ol.layer.Vector({
  id: 'europa',
  source: new ol.source.Vector({
    format: new ol.format.GeoJSON(),
    url: '../assets/data/nutsv9_lea.geojson'
  }),
  style: defaultEuropa
});

另请注意,ol.source.Vector 没有 projection 选项。

如果你想创建一个空源,你不应该设置 format:

var bbox = new ol.layer.Vector({
  source: new ol.source.Vector()
});

要将特征添加到上面的源中,您需要在视图投影中使用几何图形创建它们,例如使用 bbox.getSource().addFeatures.