[GIS] OpenLayers GeoJSON t.getState is not a function

openlayers

I'm trying to make my own example with Openlayer. I have to load a GeoJSON on my map. But it doesn't work and I have a message on the console where is written : Uncaught TypeError: t.getState is not a function

   var geojsonObject = {
     'type': 'FeatureCollection',
     'crs': {
       'type': 'name',
       'properties': {
         'name': 'EPSG:3857'
       }
     },
     'features': [{
     'type': 'Feature',
     'geometry': {
       'type': 'Point',
       'coordinates': [0, 0]
       }
     }]
   };

 var masource = new ol.layer.Vector({
     features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
   });

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

   var map = new ol.Map({
     target: 'map',
     layers: [
       new ol.layer.Tile({
        source: new ol.source.OSM()
       }),monlayer
     ],
     view: new ol.View({
       center: [0, 0],
       zoom: 2
     })
   });

I don't understand what is going wrong.

Best Answer

You are declaring a source using new ol.layer.Vector( instead of new ol.source.Vector(

Replace

var masource = new ol.layer.Vector({
  features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

with

var masource = new ol.source.Vector({
  features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

PS :tested :)

Related Question