JavaScript How do I get features from a geoJson load? features geojson,features geojson format,features geojson polygon,openlayers add

I've got some GeoJson loading into an openlayers 3 vector layer

var countriesLayer = new ol.layer.Vector({
  source: new ol.source.Vector({
    url: '/data/countriesandstates.geojson',
    format: new ol.format.GeoJSON()
  }),
  style: function(feature, resolution) {
    countriesLayerTextStyle.getText().setText(resolution < 5000 ? feature.get('name') : '');
    return [countriesLayerStyle, countriesLayerTextStyle];
  }
});

I want to run over all the features in that source using

countriesLayer.getSource().forEachFeature(...);

However it never calls my callback, and if I try getFeatures() I get an empty array back. However it renders just fine so I know the data is loaded. I even tried doing in 5 seconds later on a timeout to make sure it was loaded and parsed.

What am I doing wrong?

Answer:1

This seems to work:

countriesLayer.getSource().on("change", function(ev) {
    if( countriesLayer.getSource().getState() === "ready" ) {
        console.log(countriesLayer.getSource().getFeatures().length)
    }
});
Answer:2

When there is 10+ series in a Scatter plot for examples - Legend look really bad.. It just extends to bottom of the screen - crossing chart area etc. What it would be nice is to have a drop-down list ...

When there is 10+ series in a Scatter plot for examples - Legend look really bad.. It just extends to bottom of the screen - crossing chart area etc. What it would be nice is to have a drop-down list ...

Whenever I run the script it returns NaN for audio duration. Script var mytrack = document.getElementById('mytrack'); var duration1 = document.getElementById('fullDuration'); duration1.innerHTML = ...

Whenever I run the script it returns NaN for audio duration. Script var mytrack = document.getElementById('mytrack'); var duration1 = document.getElementById('fullDuration'); duration1.innerHTML = ...

  1. audio duration returns nan

I'm building an Angular directive which allows the user to enter a North American phone number in a variety of common formats (such as "1(301) 797-1483" or "301.797.1483"), but stores it internally as ...

I'm building an Angular directive which allows the user to enter a North American phone number in a variety of common formats (such as "1(301) 797-1483" or "301.797.1483"), but stores it internally as ...

Using regex I am attampting to limit the user's input to one of the following scenarios: http(s)://www.instagram.com/user www.instagram.com/user http(s)://instagram.com/user instagram.com/user ...

Using regex I am attampting to limit the user's input to one of the following scenarios: http(s)://www.instagram.com/user www.instagram.com/user http(s)://instagram.com/user instagram.com/user ...