[GIS] How to project a GeoJSON on the fly in OpenLayers 3

coordinate systemgeojsonjavascriptopenlayers

I have been trying to load a GeoJSON without success. I have a map with a background layer from OSM. I have some data in 4326 I want to overlay on that map. I do not want to transform coordinates one by one on the server-side and I'd rather have OpenLayers transform them on the fly … if possible at all.

I have tried using the "projection" attribute under the vector Layer, vector Source, main background layer, … everywhere I have thought of … to no avail: the sample point keeps showing near 0,0.

I set up a fiddle with my test code, any idea? Is it simply not possible?

http://jsfiddle.net/zzahmbff/2/

The data here is just a sample. Once I get it to work I'll load the data with AJaX (Asyncronous Javascript XML).

Best Answer

I have put a projection setting in your source definition and it seems to work:

var vS=new ol.source.GeoJSON(
    ({
        "object": data,
         projection: 'EPSG:3857'
    })
);

This is the result: http://jsfiddle.net/zzahmbff/3/

Perhaps this resource can help you to see different ways to load vector data: http://acanimal.github.io/thebookofopenlayers3/chapter03_03_vector_source.html