Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
Just as a preliminary diagnostic: are you getting any JS errors in the browser console? –  yalestar Jan 28 '13 at 16:53
    
no, nothing shows in the console –  Munzilla Jan 30 '13 at 16:30
    
Just a thought: add layer to map; then add features to layer –  user1702401 Aug 14 '13 at 10:02

1 Answer 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...

share|improve this answer
    
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 Jan 28 '13 at 20:00
    
did you try what i have mentioned? –  Aragon Jan 29 '13 at 8:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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