Sunday 2 July 2017

Load GeoJSON file in OpenLayers 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?



Answer



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


layer.getSource().getExtent();

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...