Sefran2
Sefran2

Reputation: 3588

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

In OpenLayers 3.13v, I obtain Uncaught AssertionError: Assertion failed: format must be set when url is set with ol-debug.js, while Uncaught TypeError: Cannot read property 'V' of undefined with ol.js

I use the following code by replacing 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
  });

Moreover, I have the same issue if I try to create an empty layer like in this example

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

Upvotes: 1

Views: 718

Answers (1)

ahocevar
ahocevar

Reputation: 5648

You have to pass an instance to the source's format option:

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

Also note that there is no projection option for ol.source.Vector.

If you want to create an empty source, you should not be setting a format:

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

To add features to the source above, you will need to create them with geometries in the view projection, and e.g. use bbox.getSource().addFeatures.

Upvotes: 1

Related Questions