[OpenLayers-Users] Cluster strategy not working

Jerome Freyre jerome.freyre at hispeed.ch
Fri Aug 28 10:19:18 EDT 2009


Heidtmare is right and faster than me.... :D


Here is a script working

	        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.
> 
> 
> -----Original Message-----
> From: users-bounces at openlayers.org on behalf of Shawn Oatley
> Sent: Fri 8/28/2009 9:26 AM
> To: x.zam at gmx.net; users at openlayers.org
> Subject: Re: [OpenLayers-Users] Cluster strategy not working
>  
> I don't have any input regarding the code, but I have worked with IE 8. 
> Not sure if that is an option, but if it is, they have a powerful script
> debugger that allows you to step through the code also.
> 
> Shawn
> 
>>>> Max Stephan <x.zam at gmx.net> 08/28/09 8:24 AM >>>
> 
> Hi Jerome,
> 
> I´d be very happy if I could use Firebug in this case (very nice tool
> for
> debugging and for tracking the DOM in my opinion) but as I mentioned
> above:
> I´m using an ActiveX-Control for reading out the Access-Database and for
> sending further requests to it. And this method is only possible in
> Internet
> Explorer for which I haven´t found such a powerful debugging tool as
> firebug
> (firebug lite wasn´t useful).
> 
> Ok, now here´s the loop, which is adding the vector-features to my
> vector-layer:
> for (var i=0; i<mestArray.length; i++){
>     addVector(vectorLayer, mestArray[i][1], mestArray[i][0]);
> }
> The first parameter is the target-layer, the second is a lonlat-object
> and
> the third is an attribute for the features.
> 
> And here´s the addVector-function:
> function addVector(layer, lonlat, mestId){
>    var geometryObjekt = new
> OpenLayers.Geometry.Point(lonlat.lon,lonlat.lat);
>    var vectorObjekt = new OpenLayers.Feature.Vector(geometryObjekt);
>    vectorObjekt.attributes.mest = mestId;
>    layer.addFeatures([vectorObjekt]);
> }
> 
> And concerning your english: No need to be worried. I´m understanding
> everything.
> 
> Thx in advance
> Max Stephan
> 
> Jerome Freyre wrote:
>> 
>> Can you also show us your method that parse and add the feature to the
>  
>> layer?
>> 
>> I think another good way to explore is to use firefox and firebug.
>> 
>> With firebug, you can add breakpoints and the go step-by-step...
>> 
>> 
>> Whith that, you should put a breakpoint into your method that parse  
>> and add features to layer and another one into the cluster strategy  
>> (Think to not use the OpenLayers minimised but the complete source) to
>  
>> see if software run into the class correctly...
>> 
>> 
>> Hope my english is quite understandable.... :)
>> 
>> 
>> 
>> Jérome
>> 
>> 
>> 
>> Le 28 août 09 à 14:01, Max Stephan (via Nabble) a écrit :
>> 
>>> Hi Jerome,
>>>
>>> thanks for your reply. I already tried that but it causes an error  
>>> ("this.layer.events" is null or no object) and the map content isn 
>>> ´t shown.
>>>
>>> Any other ideas?
>>>
>>> Greets,
>>> Max Stephan
>>> Jerome Freyre wrote:
>>> Hi Max,
>>>
>>> Personnally, when I use Cluster Strategy, I create separatly the  
>>> strategy and the I activate it.
>>>
>>> 		
>>> 			var strat = new
> OpenLayers.Strategy.Cluster({distance:20});
>>> 			vectorLayer = new
> OpenLayers.Layer.Vector("Vektorlayer",{
>>> 			strategies: [
>>> 				strategy
>>> 			],
>>> 			styleMap: vectorStyle2, maxResolution: 19});
>>> 			strat.activate();
>>>
>>>
>>> Hope it will be usefull....
>>>
>>> Sincerly,
>>> Jérome
>>>
>>> Max Stephan wrote:
>>> Hi everybody,
>>>
>>> I`m having problems getting the cluster strategy to work. It simply  
>>> makes no difference in terms of appearance if I add it to the  
>>> VectorLayer or not.
>>>
>>> This is my code for the layer:
>>> 		vectorLayer = new
> OpenLayers.Layer.Vector("Vektorlayer",{
>>> 			strategies: [
>>> 				new
> OpenLayers.Strategy.Cluster({distance:20})
>>> 			],
>>> 			styleMap: vectorStyle2, maxResolution: 19});
>>>
>>>
>>> The reason why I want to add it is that I have 480 Features  
>>> alltogether on my map and even when zooming into some areas there  
>>> are still around 150 features left in the bbox causing  
>>> InternetExplorer to slow down (don´t know if it´s the same case  
>>> with Firefox, but I gotta use IE to use ADODB-Connection to an  
>>> Access Database in the Intranet).
>>>
>>> Those features are created and added step by step with an addVector- 
>>> method I have implemented that reads out the coordinates and  
>>> attributes of the database for a single feature and then jumps to  
>>> the next entry and repeats the procedure till all features have been 
> 
>>> added.
>>>
>>> greets and thx in advance
>>> Max Stephan
>>>
>>>
>>> View message @
>>>
> http://n2.nabble.com/Cluster-strategy-not-working-tp3533851p3534202.html
>>> To unsubscribe from Re: Cluster strategy not working, click here.
>>>
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://n2.nabble.com/Cluster-strategy-not-working-tp3533851p3534315.html
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 
> 
> 
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
> 
> 

-- 
View this message in context: http://n2.nabble.com/Cluster-strategy-not-working-tp3533851p3534984.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.



More information about the Users mailing list