2

I'm trying to add features from a GeoJSON response in openlayers3. Using the default loading method works great:

layer.setSource(new ol.source.Vector({url:url, format:new ol.format.GeoJSON()});

But I need error handling, So I use the response from a standard jquery loader like so:

var features = new ol.format.GeoJSON().readFeatures(response);
layer.setSource(new ol.source.Vector({features:features}));

All the features appear bunched on null island in a sort of a mini-map: enter image description here

And a closer look reveals the features: enter image description here

edit:Specifying the data/feature projection doesn't work either.

EDIT: I finally fixed it, Here's the code:

var reader = new ol.format.GeoJSON({
    defaultDataProjection:'EPSG:3857',
    Projection:'EPSG:3857'
});
layer.getSource().addFeatures(reader.readFeatures(res,{
    dataProjection:'EPSG:4326',
    featureProjection:'EPSG:3857'
}));

What an absolute nightmare that was.

5
  • You say "Specifying the data/feature projection doesn't work either.", but did you specify the correct projection? This is probably a WGS84 vs Google Mercator problem ;) Feb 21, 2017 at 14:53
  • The projection is EPSG:4326 WGS84. I verified it with QGIS, Exported it again just in case, Added a crs member to the GeoJSON response and defined both data and feature projection as EPSG:4326. It didn't work.
    – shepp
    Feb 21, 2017 at 16:34
  • How did you specify the projection in OL? Try gis.stackexchange.com/a/148593/51035 . Your map is probably in 3857 while your GeoJSON is 4326. Feb 21, 2017 at 20:03
  • The extent I'm getting is [ -171.791111, -18.347975, 145.543137, 71.357764 ]. I changed the projection to EPSG:3857 and nothing really changed.
    – shepp
    Feb 22, 2017 at 17:06
  • Thank you very much. Messing with OL 4 is also a nightmare, the docs have NO examples.
    – niahoo
    Nov 29, 2017 at 23:42

1 Answer 1

2

I fixed it by playing with the projection options:

var reader = new ol.format.GeoJSON({
defaultDataProjection:'EPSG:3857',
Projection:'EPSG:3857'
});
layer.getSource().addFeatures(reader.readFeatures(res,{
    dataProjection:'EPSG:4326',
    featureProjection:'EPSG:3857'
}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.