[OpenLayers-Users] Looping through unique markers?

David Raasch dr4296 at myoldhouse.com
Fri Feb 27 09:12:46 EST 2009


Greetings All!

When I posted to the list last week, I had coded my little app to use
FEATURES to draw marker icons and rig-up word-balloon pop-ups whenever they
were clicked-on.

However, the markers took a very, very long time to load.  So, digging
further into the archives of this discussion list, I discovered that,
although using MARKERS to draw marker icons is considered to be "the old way
of doing it", that way is apparently faster.

So, I set about commenting-out the feature-adding/drawing lines in my code
and inserting some lines that would loop through my array of data,  draw
each marker, and register an event for that marker.

My problem is that I'm only getting the event to work on the FINAL marker in
the array.

I've posted my files here:

http://www.davidraasch.com/ww/map_v4.html


If you zoom in over that cluster near Chicago and click, eventually you'll
trigger an alert message on the one marker, which is the last one in the
array.  It will say "You clicked on marker m571".

Feel free to do a "view source", but here are the pertinent lines from my
code:



var markers_layer = new OpenLayers.Layer.Markers( "Markers Layer");
map.addLayer(markers_layer);

	var xml_points =
xmlDoc.documentElement.getElementsByTagName("point");
		  		var m = [];
		  		for (var i = 0; i < xml_points.length; i++)
{
		         		var type =
xml_points[i].getAttribute("type");
		    	 	 	var thislat =
(parseFloat(xml_points[i].getAttribute("lat")));
	    		  		var thislng =
(parseFloat(xml_points[i].getAttribute("lng")));
	    		  		var html =
xml_points[i].getAttribute("html");

{to reduce "noise" here for you, I'm leaving out the if-then statements that
determine which exact icon graphic file to use, plus its height and width }


	// Code to push each point as a MARKER LAYER MARKER
			  
					var size = new
OpenLayers.Size(icon_width,icon_height);
            			var offset = 0; // new
OpenLayers.Pixel(-(icon_width/2), -icon_height);
            			var icon = new
OpenLayers.Icon(icon_image,size,offset);
            			
   		   			
            			 m[i] = new OpenLayers.Marker((new
OpenLayers.LonLat(thislng,thislat).transform(new
OpenLayers.Projection("EPSG:4326"), map.getProjectionObject())),icon);
            			 markers_layer.addMarker(m[i]);
   		  			 m[i].events.register ('click',
m[i], function(evt) {alert ('You clicked on marker m'+ i);
OpenLayers.Event.stop (evt); })

           				// alert("i="+ i);
				} // end of "for i = 0  to all POINTS
				



(Originally, I was using just the variable "m1" to represent the markers, as
taken from an example I found somewhere on the openlayers.org site.  But
then, in debugging it, I switched to using "m" as an array, just to make
sure I did understand what we're trying to do here.)

I should also mention that, for some reason, the markers don't even appear
to load in Firefox!  I can only see them in IE.  ????

Any thoughts?


Thanks!

-= Dave =-






More information about the Users mailing list