I'm working on a map with WMS overlays and a lot of markers, my problem is when I try to calculate the average of points clustered, I've found how to differentiate the behaviour and the label text depending on the number of clustered elements, but I don't know how to reach the "val" value of every clustered point to calculate their average and show in the clustered point.

I think values are into features.cluster array (if I look into it with .toString() I get different numbers of [object][object],[object][object] etc), but I can't understand how to reach those values.

This is the context code:

context: {
    val: function(feature) { 
       if(feature.attributes.count>1) {
           [here I should show the average]
       } else return feature.attributes.val; 

And this is just some from the OpenLayers.Style code:

var pointStyle = new OpenLayers.Style({
    strokeWidth: "${strokeFunction}",
    fillColor: "${fillFunction}",
    label: "${val}",

Don't think it's needed, but this is the point code where I set the val variable:

var pointFeature = new OpenLayers.Feature.Vector(point);
     pointFeature.attributes = {
         val: mks[i].m,
         align: "cm"

(mks[i].m is the value I get from a JSON array, it's a number with decimals)

I really appreciate any help you can provide, Alberto


As you mentioned, the feature.cluster array contains all the information, you can iterate over the array and get attribute value from each element. You can access to each element's attribute value by feature.cluster[i].attributes['val']. The code in your if block would be something like:

var sumValue = 0;
for (var i=0; i<feature.cluster.length; i++) {
    sumValue += feature.cluster[i].attributes['val'];
var averageValue = sumValue/feature.cluster.length;
return averageValue;
| improve this answer | |
  • Great! It works, thanks a lot, I've tried everything but treating attributes like an array. Thanks a lot! – Alberto Dec 7 '13 at 19:14

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.