0

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( ??? ); 
Moonsera
  • 35
  • 1
  • 6

3 Answers3

1

I don't think this is possible. I also don't think this is possible in standard OpenLayers with the standard AnimatedCluster.

Your best guess is to first go to https://github.com/acanimal/AnimatedCluster and ask there if what you want is possible (in standard openlayers).

If they say it is possible, and say how come back here and I can look further into it. If they say it is also not possible in standard openlayers, then it is also not possible in gwt openlayers.

Knarf
  • 2,077
  • 1
  • 16
  • 24
  • It's possible in JS according to this article: http://openflights.org/blog/2009/10/21/customized-openlayers-cluster-strategies/ (look at "The most important strategy" section). If only I knew how to attach function the same way in gwt-openlayers. – Moonsera Feb 10 '14 at 14:10
  • I believe the example you provide uses the standaard clustering in OpenLayers. You are trying to use Animated Cluster which is a 3rd party addon with different posibilities. This is a GWT-Openlayers example with the default cluster strategy : http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Cluster%20Strategy%20Threshold Looking a bit further. That animated cluster extends the normal cluster in JS. So probably everything possible in normal cluster is possible in animated. IF I have time I will look into it. Maybe for now try with normal Cluster strategy. – Knarf Feb 11 '14 at 07:46
  • I already switched to standard clustering, and for example I provided it doesn't matter which strategy is used. – Moonsera Feb 11 '14 at 07:49
  • I'll look into it when I have some time. – Knarf Feb 11 '14 at 07:55
  • Thanks, will be much appreciated. Now I'm searching how to implement inline JS functions in GWT. – Moonsera Feb 11 '14 at 08:07
1

I enhanced the Animated Cluster With Popup example to do what you ask. Will take some time before this is online though.

Changes I made are :

First I added an attribute to each feature added to the map. This feature just is a random number that we want to display the sum of when clicking a clustered feature :

for (int i = 0; i < points.size(); i++)
{
   features[i] = new VectorFeature(points.get(i));
   Attributes attributes = new Attributes();
   attributes.setAttribute("examplenumber", Random.nextInt(10));
   features[i].setAttributes(attributes);
} 

Second change is in the public void onFeatureSelected(FeatureSelectedEvent eventObject)

int totalNumber = 0;
VectorFeature[] clusters = eventObject.getVectorFeature().getCluster();
for (int i = 0; i < clusters.length; i++)
{
   GWT.log("examplenumber = " + clusters[i].getAttributes().getAttributeAsInt("examplenumber"));
   totalNumber += clusters[i].getAttributes().getAttributeAsInt("examplenumber");
}

Now totalnumber contains the sum of all examplenumber attribute values.

I believe this solves your problem ?

Knarf
  • 2,077
  • 1
  • 16
  • 24
  • And showcase is now also updated : http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Animated%20cluster%20with%20popup – Knarf Feb 11 '14 at 10:17
  • I made it the other way, see my answer. – Moonsera Feb 11 '14 at 11:34
0

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;
}-*/;
Moonsera
  • 35
  • 1
  • 6
  • Using native methods is always a possible solution. But the goal of gwt-openlayers is that you don't need to use native methods. Anyway, good to see that the problem is solved :) – Knarf Feb 11 '14 at 12:12