[OpenLayers-Users] Cluster strategy not working
Jerome Freyre
jerome.freyre at hispeed.ch
Fri Aug 28 10:23:13 EDT 2009
Heidtmare is right and faster than me.... :D
Have a look to http://openlayers.org/dev/examples/strategy-cluster.html to
modify the size of cluster depending of the number of feature they represent
Here is a script that is working. And think that you can
PS: Sorry for the "double post" I push the wrong button :)
var map, wfs;
var styleMap1 = new OpenLayers.StyleMap({
"default": new OpenLayers.Style(
{
fillColor: "#0000FF",
strokeColor: "#0000FF",
pointRadius: 6
}
)
});
function init(){
map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0?",
{layers: 'basic'}
);
map.addLayer(wms);
map.zoomToMaxExtent();
var strat = new OpenLayers.Strategy.Cluster({distance:20});
vectorLayer = new OpenLayers.Layer.Vector("Vektorlayer",{
strategies: [
strat
],
styleMap: styleMap1, maxResolution: 19});
strat.activate();
map.addLayers([vectorLayer]);
/**
* Generate datas // Simulate your data access
*/
var dx = 3;
var dy = 3;
var px, py;
featuresToBeClustered = [];
for(var x=-45; x<=45; x+=dx) {
for(var y=-22.5; y<=22.5; y+=dy) {
px = x + (2 * dx * (Math.random() - 0.5));
py = y + (2 * dy * (Math.random() - 0.5));
featuresToBeClustered.push({
'lonlat':new OpenLayers.LonLat(px,py),
'id':px+' '+py
});
}
}
/**
* Populate the array of feature
*/
var arrayOfFeatures = [];
for (var i=0; i<featuresToBeClustered.length; i++){
arrayOfFeatures.push(generateFeature(vectorLayer,
featuresToBeClustered[i].lonlat, featuresToBeClustered[i].id));
}
/**
* Add all features in one time
*/
vectorLayer.addFeatures(arrayOfFeatures);
map.setCenter(new OpenLayers.LonLat(0, 0), 2);
}
/**
* Modification of you function
*/
function generateFeature(layer, lonlat, mestId){
var geometryObjekt = new
OpenLayers.Geometry.Point(lonlat.lon,lonlat.lat);
var vectorObjekt = new OpenLayers.Feature.Vector(geometryObjekt);
vectorObjekt.attributes.mest = mestId;
return vectorObjekt;
}
Heidtmare wrote:
>
> i believe you need to add all your features at once,
> so instead of looping the layer.addFeatures() call
> you need to build all your features into an array and then call
> layer.addFeatures() once.
>
>
>
--
View this message in context: http://n2.nabble.com/Cluster-strategy-not-working-tp3533851p3535010.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
More information about the Users
mailing list