3

Before, to load my .geojson, I had a php file who generated the code :

var vector_limite_comm_white = new ol.layer.Vector({
    source: new ol.source.GeoJSON({
        url: 'geojson1.php',
        defaultProjection :'EPSG:4326', projection: 'EPSG:3857'
    }), 
    name: 'NAME 1',
    style: style_white()
})

After, I tried to directly load my geojson file, so I wrote :

var vector_name1    = new ol.layer.Vector({
    source: new ol.source.Vector ({
        url: 'data/json/file1.geojson',
        defaultProjection :'EPSG:4323', projection: 'EPSG:3857'

    }),
    name: 'NAME 1',
    style: style_white()
})

In firebug, at onload event, I saw my geojson file was loaded correctly but, it doesn't appear in my map. So I tried an other method like that :

var test = 'data/json/file1.geojson';
var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON()).readFeature(test)
}); 
var vector_overlay = new ol.layer.Vector({
    source: vectorSource,
    style: new ol.style.Style(style_white()
});

It's not a good method too.

Do you have some explications?

1

You must have a issu with your projection. Try getting the extent of your json layer :

layer.getSource().getExtent();
  • Where do you write this part of code ? – Geo-x May 27 '15 at 6:40
  • use your firebug, get hold of your layer using : map.getLayers().getArray()[x] – julsbreakdown May 27 '15 at 6:47
  • If I test the getSource().getExtent() code, I've, with the code console.log, the result is : [Infinity, Infinity, -Infinity, -Infinity] ... – Geo-x May 27 '15 at 7:09
4

You gave me a good idea with this code jules and mapperz.

The problem was the not good crs of my Geojson.

So the good code for a version of OpenLayers < 3.5 is :

var layer1=new ol.layer.Vector({ source: new ol.source.GeoJSON ({ url: 'data/json/mydata.geojson', defaultProjection :'EPSG:4326', projection: 'EPSG:3857' }), name: 'NAME 1', style: style_1() })

For the version 3.5.0 that's it :

var layer1=new ol.layer.Vector({

   source: new ol.source.Vector({

      url: 'data/geojson/mydata.geojson',
      format: new ol.format.GeoJSON({

         defaultDataProjection :'EPSG:4326', 
         projection: 'EPSG:3857'

      })

   }),
   name: 'NAME 1',
   style: style_1()
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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