Search code examples
gisgeojsonopenlayers-3

Openlayers 3.5 and GeoJSON


I'm having a problem with OpenLayers 3.5. I'm trying to use the one-off loading strategy to grab features from a GeoJSON file. I'm adding a new layer to an already instantiated map. My code looks like this:

var vectorSource = new ol.source.Vector({
  url: layerInfo.url,
  format: new ol.format.GeoJSON()
});

var pointsLayer = new ol.layer.Vector({
  source: vectorSource,
  style: styleFunc
});

that.map.addLayer(pointsLayer);
pointsLayer.setVisible(true);

However, nothing shows, and when I examine pointsLayer.getSource().getFeatures(), I discover that no features were actually loaded.

So, now I tried to load the features a different way:

var that = this;

$.get(layerInfo.url, function(response) {

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

  var pointsLayer = new ol.layer.Vector({
    source: vectorSource,
    style: styleFunc
  });

  that.map.addLayer(pointsLayer);
  pointsLayer.setVisible(true);
});

This DOES work. I'm banging my head against a wall here. Does anyone have any ideas? Thanks so much!


Solution

  • This is how I'm loading the data now, "data" is my GJson

    var wktTraffic = new ol.source.Vector({
    });
    
    var trafficLayer = new ol.layer.Vector({
        source: wktTraffic,
        style: new ol.style.Style({
                stroke: new ol.style.Stroke({
                color: 'blue',
                width: 5
            })
        })
    });
    
    function showData(data) {
        var format = new ol.format.WKT();
        var feature;
        $.each(data, function (i, link) {
            feature = format.readFeature(link.geom);
            wktTraffic.addFeature(feature);
        })
        console.log('done load map');
    }