2

It seems like OpenLayers library changes from day to day and loses compatibility with the previous minor version each time... But, actually, I have another question to ask. The problem I face, is that I'm unable to load data from url. In previous versions I did it like so:

var vectorSource = new ol.source.GeoJSON({
  projection: 'EPSG:3857',
  url: '../assets/data/countries.geojson'
});
var vectorLayer = new ol.layer.vector({
  source: vectorSource
 });

Now, there are no longer any ol.source.GeoJSON class and no ol.layer.vector class (with ol.layer.Vector instead). So, how to implement it? The second problem I face, is that I'm unable to create and display vector layer from local geojson object. I know how they do it now and I did it in the same manner:

var geojsonObject = { 
"type": "FeatureCollection",
"features":[{
    "type": "Feature",
    "geometry": {
        "type": "Point", 
        "coordinates": [82.0, 0.5]
    },
    "properties": {"prop0": "value0"}
},{ 
    "type": "Feature",
    "geometry":{
        "type": "LineString",
        "coordinates": [
            [102.0, 0.0], 
            [103.0, 1.0], 
            [104.0, 0.0], 
            [105.0, 1.0]
        ]
    },
    "properties": {
        "prop0": "value0",
        "prop1": 0.0
    }
},{ 
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
          [ [100.0, 0.0], 
            [101.0, 0.0], 
            [101.0, 1.0],
            [100.0, 1.0], 
            [100.0, 0.0] ]
        ]
    },
    "properties": {
       "prop0": "value0",
       "prop1": {"this": "that"}
    }
}]
};

var vectorSource = new ol.source.Vector({
  features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

var vectorLayer = new ol.layer.Vector({
  source: vectorSource
});

map.addLayer(vectorLayer);

But when I run this code, I get only some default blue circle in the very centre of the map (no errors and no warnings in the console, but still some wrong behaviour).

I wonder what is the right way to do it?

  • Did you ever find a solution to this problem? – phanf Jun 6 '17 at 15:40

Your Answer

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

Browse other questions tagged or ask your own question.