gwtlabelgwt-openlayers

GWT OpenLayers set sum of values of underlying VectorFeatures on cluster point


I'm using gwt-openlayers-1.0, currently learning this example (Animated Cluster Strategy).

In my project each VectoreFeature has a numeric label and I want to display sums of label values of underlying points on each cluster point. Is there a way to do that?

upd:
According to this article (The “Most Important” Strategy part) in JS it would look like this:

// for each feature:
feature.attributes = { result_count: 10 };
...
var style = new OpenLayers.Style({
  ...
  } , context: {
    label: function(feature) {
      if (feature.cluster) {
        var result_count = 0;
        for (var i = 0; i < feature.cluster.length; i++) {
          result_count += feature.cluster[i].attributes.result_count;
        }
        features.attributes.label = result_count.toString();
      } else {
        features.attributes.label = features.attributes.result_count.toString();
      }
    }
  }

But I can't find a way to implement this in gwt-openlayers:

org.gwtopenmaps.openlayers.client.Style style = new org.gwtopenmaps.openlayers.client.Style();
style.setLabel( ??? ); 

Solution

  • In method where I assign strategy to the VectorLayer:

    {
        org.gwtopenmaps.openlayers.client.Style style = new org.gwtopenmaps.openlayers.client.Style();
        style.setJSObject(getOpenLayersStyle());
    }
    

    And where the magic is done:

    private native JSObject getOpenLayersStyle() /*-{
        var style = new $wnd.OpenLayers.Style({
            fontColor: "#FFFFFF",
            fontSize: "12px",
            label: "${countLabel}"
            }, { context: {
                countLabel: function(feature) {
                    var countLabel;
                    if (feature.cluster) {
                        var result_count = 0;
                        for (var i = 0; i < feature.cluster.length; i++) {
                            result_count += feature.cluster[i].attributes.result_count;
                        }
                        countLabel = result_count.toString();
                    } else {
                        countLabel = feature.attributes.result_count.toString();
                    }
                    feature.attributes.label = countLabel;
                    return countLabel;
                }
            }
        });
        return style;
    }-*/;