1

I need help concerning feature clustering in OpenLayers 3. I have a vector layer that dynamically "collects" a large number of features. All features have various properties like title, content, permalink.

Unfortunately, I only have a clue how to copy style data from the source layer (for all features that are not included into clustering at the respective zoom level).

How do I get all the other properties into the features at the cluster layer?

I would like to add feature.get('permalink') and so on, but I don't have any idea where I have to put this.

Here's my code:

// marker clustering
    clusterLayer[i] = new ol.layer.Vector({
        source: clusterSource[i],
        style: function(feature) {
          var size = feature.get('features').length;
          if(size > 1) {
              var style = new ol.style.Style({
                  image: new ol.style.Circle({
                    radius: 10,
                    stroke: new ol.style.Stroke({
                      color: '#fff'
                    }),
                    fill: new ol.style.Fill({
                      color: '#3399CC'
                    })
                  }),
                  text: new ol.style.Text({
                    text: size.toString(),
                    fill: new ol.style.Fill({
                      color: '#fff'
                    })
                  })
              });
          }
          else var style = feature.get('features')[0].getStyle();
          return style;
        }
    });

Any idea how I can add the properties from all source layer features to the features at the cluster layer?

  • did you try feature.get('features')[0].get('permalink') ????? – pavlos Jan 26 '17 at 11:03
  • Hi pavlos, thanks, yes, I did. But inside the "style" function I can't save properties that don't belong to (CSS) styles, unfortunately. Ich think I would have to find another function to loop through all features. I tried getProperties: function(feature){} but that does not work. – winnewoerp Jan 26 '17 at 12:43
  • This is a matter of your programming logic. You want to get the feature attributes. Right? When? when pressing a button? Maybe yes , maybe not. feature.get('features')[0].get('permalink') will give you the attributes if you use it within your logic. Unless you explain your logic dont expect us to guess your logic. – pavlos Jan 26 '17 at 13:09
  • Yes, I want to get the feature attributes. But I need to "transfer" them from the vector layer into which the features are loaded dynamically to the cluster layer - and I simply did not manage to do this with other than style properties so far. – winnewoerp Jan 26 '17 at 17:49
  • ...ooops, submitted too early, I continue: I'm sorry, maybe I'm just blind - or not able to explain my problem in an understandable way. Where would I put your helpful line of code inside the above snippet? Not to forget: I need the feature data when opening a popover that was originally connected to the basic vector layer, but it now has to get the feature data from the feature copies in the cluster layer. This, for some reason, does not work so far. – winnewoerp Jan 26 '17 at 18:01
1

Here is a code snip to help you out:

map.on('click', function(e){
  var attsCollector = [];
  map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
     //you may also use the layer argument here, so avoid processing for layers you dont want such functionality
    if (typeof feature.get('features') === 'undefined') {
      // means there is no cluster feature on click
    } else {
      var clustFeats = feature.get('features');
      //now loop through the features to get the attributes
      //I have just added the "attr1" for demo purposes
      for(var i = 0; i < clustFeats.length; i++) {
       attsCollector.push(clustFeats[i].get("attr1"));
      }
     }
  });
  //now rise your popup and populate it with the
  //collected attrs
  alert("number of features for clicked  cluster====="+attsCollector.length)
  console.log(attsCollector);
})

An here is a fiddle to see it in action

  • Thanks for your answer. It doesn't solve my problem directly, but is helpful in other cases. – winnewoerp Jan 27 '17 at 14:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.