2

I've built a mapping solution using OpenLayers that works well except I can't get the points to cluster. I've looked at the examples on the site, and I've even implemented those examples in my environment to make sure they work. When I try to do it with my map, however, all of my points show up, but they're not clustered.
Here is my code where I create the Vector Layer:

var layerVector = new OpenLayers.Layer.Vector("Regions",{
                 strategies: [

                        new OpenLayers.Strategy.Cluster()
                    ]
            });

I add all of the points to an array of features:

var feature_point = new OpenLayers.Feature.Vector(
                            this, {
                                "rName" : rObj.tName
                            }, {
                                'fillColor' : rObj.rColor,
                                'strokeWidth' : borderWidth,
                                'strokeColor' : rObj.rColor,
                                'fillOpacity' : regOp,
                                'pointRadius' : regPercent
                            });
                            fArray.push(feature_point);

Add the features:

layerVector.addFeatures(fArray);

And add the layer to the map:

window[mapName].addLayer(layerVector);

Seems pretty simple, but it seems like I'm missing something. In the example on the OpenLayers site, they add a styleMap to the layer, but I tried this and it doesn't do anything for me. I'm honestly not sure what it even means:

 var style = new OpenLayers.Style({
                    pointRadius: "${radius}",
                    fillColor: "#ffcc66",
                    fillOpacity: 0.8,
                    strokeColor: "#cc6633",
                    strokeWidth: "${width}",
                    strokeOpacity: 0.8
                }, {
                    context: {
                        width: function(feature) {
                            return (feature.cluster) ? 2 : 1;
                        },
                        radius: function(feature) {
                            var pix = 2;
                            if(feature.cluster) {
                                pix = Math.min(feature.attributes.count, 7) + 2;
                            }
                            return pix;
                        }
                    }
                });

                strategy = new OpenLayers.Strategy.Cluster();

                clusters = new OpenLayers.Layer.Vector("Clusters", {
                    strategies: [strategy],
                    styleMap: new OpenLayers.StyleMap({
                        "default": style,
                        "select": {
                            fillColor: "#8aeeef",
                            strokeColor: "#32a8a9"
                        }
                    })
                });

Any help would be immensely appreciated.

3
  • Just as a preliminary diagnostic: are you getting any JS errors in the browser console?
    – yalestar
    Commented Jan 28, 2013 at 16:53
  • no, nothing shows in the console
    – Munzilla
    Commented Jan 30, 2013 at 16:30
  • Just a thought: add layer to map; then add features to layer Commented Aug 14, 2013 at 10:02

1 Answer 1

1

i think you have to add your features to cluster layer that you dont need layerVector.

clusters.addFeatures(features);

and your features should be

features.push(new OpenLayers.Feature.Vector(
                     new OpenLayers.Geometry.Point(px, py), null, null // or your style
                    ));

i hope it helps you...

2
  • Well I think you're referencing the bottom bit of code in terms of the top bit. That last bottom bit of code is the example code given by OpenLayers.
    – Munzilla
    Commented Jan 28, 2013 at 20:00
  • did you try what i have mentioned?
    – urcm
    Commented Jan 29, 2013 at 8:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.