[OpenLayers-Users] Adding Features to Vector Array - The Time it Takes

Donald Kerr donald.kerr at dkerr.co.uk
Tue Jan 13 08:56:49 EST 2009


Hi list,

I am using the following code to cycle through an array and then I add the
features array to an OpenLayers Vector layer:


//======================================
	for(var i=0; i<nodes.length; ++i) {
		var uniqueID =
nodes[i].getElementsByTagName('UniqueID')[0].firstChild.nodeValue;
		var category
=nodes[i].getElementsByTagName('Category')[0].firstChild.nodeValue;
		var description =
nodes[i].getElementsByTagName('Description')[0].firstChild.nodeValue;
		var easting =
nodes[i].getElementsByTagName('GridRef')[0].getElementsByTagName('Easting')[
0].firstChild.nodeValue;
		var northing =
nodes[i].getElementsByTagName('GridRef')[0].getElementsByTagName('Northing')
[0].firstChild.nodeValue;
		var lonLat = new OpenLayers.LonLat(easting, northing);
		var feature = new OpenLayers.Feature.Vector(
			new OpenLayers.Geometry.Point(lonLat.lon,
lonLat.lat),
			{
				uniqueID: uniqueID,
				category: category,
				description: description
			}
		);
		features.push(feature);
	}
	myDataLayer.addFeatures(features);
//======================================

The last piece of code - myDataLayer.addFeatures(features) - works fine with
a handful of features e.g. 5 or 6 but if I try to load between 50 and 60 it
can take anywhere between 10 and 25 seconds. I can live with anything unde a
second but 25 seconds is completely unacceptable.

Having added some "alerts" to see where the slow-down occurs, it simply
appears to be in the last line of code. Is there any other way of doing this
quicker as I need to repeat this code every time the map moves i.e. remove
the features using destroy then add them with the above code based on the
changed bounds.

I realise that there's a limitation on the number of markers and the
speed/memory of the computer but there must be a quicker way.

I have implemented the same type of code using the Google Maps api and their
maps and the toggle on and of is almost instant.

Any help appreciated.

Many thanks.

Regards,

Donald




More information about the Users mailing list