Skip to content
Advertisement

What is the purpose of attribute “population” in OpenLayer Features?

In feature usage example, there is 2 attributes called population and rainfall.

...
var iconFeature = new ol.Feature({
    geometry: new ol.geom.Point([0, 0]),
    name: 'Null Island',
    population: 4000,
    rainfall: 500
});
...

What does it means? I searched around and didn’t find any information.

Advertisement

Answer

It’s an example of adding generic properties to a feature that you can then use elsewhere. The example doesn’t make it super obvious. In that example you could add another property called ‘numberOfDonkeys’ with a value of 20, and then you could use that in the click event that fires the popup.

Foe example, I can change the Feature to be this.

var iconFeature = new ol.Feature({
  geometry: new ol.geom.Point([0, 0]),
  name: 'Null Island',
  population: 4000,
  rainfall: 500,
  numberOfDonkeys: 20
});

And change the map click event to this.

// display popup on click
map.on('click', function(evt) {
  var feature = map.forEachFeatureAtPixel(evt.pixel,
      function(feature) {
        return feature;
      });
  if (feature) {
    var coordinates = feature.getGeometry().getCoordinates();
    popup.setPosition(coordinates);
    $(element).popover({
      'placement': 'top',
      'html': true,
      'content': feature.get('name') + ' Pop: ' + feature.get('population') + ' Donkeys: ' + feature.get('numberOfDonkeys')
    });
    $(element).popover('show');
  } else {
    $(element).popover('destroy');
  }
});

And you’ll see the Population and numberOfDonkeys attributes in the popup.

jsFiddle Example – https://jsfiddle.net/6vd5gtho/

Ultimately you don’t need those attributes at all, you could get rid of them, they are just examples of where you could put attributes you wanted to reuse in this way.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement