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

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

本文关键字:format GeoJSON ol with 13v issue OpenLayers      更新时间:2023-09-26

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

我通过替换此示例中的ol.source.GeoJSON使用以下代码

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

此外,如果我尝试创建一个空层,如本例所示,我会遇到同样的问题

  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 .