Pregunta

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( ??? ); 
¿Fue útil?

Solución 3

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;
}-*/;

Otros consejos

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.

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 ?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top