[GIS] Error Adding GeoJSON in Openlayers 3

geojsonopenlayersvector

I try to add GeoJSON layer to my openlayers 3 by the following code.

        new ol.layer.Vector({
            title: 'added Layer',
            source: new ol.source.GeoJSON({
               projection : 'EPSG:3857',
               url: 'poly1.json'
            })
        })

Getting the following error,
Uncaught TypeError: ol.source.GeoJSON is not a constructor

Best Answer

to load a geojson file in OL3 use :

var yourVectorSource = new ol.source.Vector({
    projection : 'EPSG:3857',
    url: 'yourFile.geojson',
    format: new ol.format.GeoJSON()
});

for the source then set it to a vector layer