Boltgolt Boltgolt - 1 year ago 48
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


is the same function as in the example, and
is a completely valid geoJSON object.

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


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,{