0

I am using OpenLayers Vector with a cluster strategy. When I use Vector.features to get the features returned as a list, it returns an array of clusters and not the array of features. This makes the features unable to be addressed individually. Does anyone know how can I get the features list while using the clustering strategy? Any help is appreciated.

(Eg. If there are 5 features in my KML which are clustered as 1, then I get vector.features.length=1. I need the original features list which will have a length of 5)

Below is the implementation of my Vector:

var maps_layer = new OpenLayers.Layer.Vector("KML", {
 strategies: [new OpenLayers.Strategy.Cluster()],
 protocol: new OpenLayers.Protocol.HTTP({
 url: "kml/maps.kml",
 format: new OpenLayers.Format.KML({
 extractStyles: true, 
 extractAttributes: true,
 })
 })
 })
thesis.events.register("loadend", this, function(){
 var my_array = maps_layer.features; //Size of this is = no.of Clusters and not features
});

Here is the documentation: http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html

taudorf
1,6652 gold badges16 silver badges29 bronze badges
asked Jul 22, 2012 at 5:47

3 Answers 3

3

You should use features property of cluster strategy object. In this example you can fetch all features of layer using the following syntax: map.layers[1].strategies[1].features.length.

answered Jul 22, 2012 at 8:23
0
1

Although it's an old question, i am putting here another answer/way by which I get the clustered features.

For this solution, you must first get the layer from map. Once getting layer you can access the features of this clustered layer as,
layer[0].features[0].cluster[0]
To understand its structure just log it to console and use firebug to view the log.

To make it more useful(as some features may be clustered and some may not) use the simple if condition as,

 for(var i=0;i<layer[0].features.length;i++){
 if(layer[0].features[i].cluster){
 //cluster exist. play with clustered features
 }else{
 //no clusters. directly access the feature
 }
}

Thanks

answered Sep 15, 2014 at 7:17
0

In openlayers 5 when trying to style icons I had to do

getIconStyle = function(feature) {
 if(feature.getProperties().features.length > 1) {
 //cluster
 } else {
 //no clusters
 }
}
layer = new VectorLayer({
 source: clusterSource,
 visible: false,
 style:getIconStyle
 });
answered Nov 12, 2018 at 4:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.