Boltgolt Boltgolt - 1 year ago 64
Javascript Question

OpenLayers can't draw geoJSON on map

I'm trying to draw some simple shapes (mostly lines) on a Open Street Map using OpenLayers 3. The code I’m using is almost directly copied from an example on their site, but it doesn't seem to work for me.

var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON)
});

var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});

map.addLayer(vectorLayer);


Where
styleFunction
is the same function as in the example, and
arr[i].geoJSON
is a completely valid geoJSON object.

The problem is, that it doesn't draw anything. What am I doing wrong?

Answer Source

You probably need to provide options to the readFeatures method. GeoJSON projection is generally using 4326 while most web map are generally in 3857.

Try to call:

(new ol.format.GeoJSON()).readFeatures(arr[i].geoJSON,{
    featureProjection:"EPSG:3857"
});