[OpenLayers-Users] why the popups have same content?

Yves Gillet ygillet at gmail.com
Thu Feb 28 05:45:04 PST 2013


Hi

because you hit a classical problem with closures inside loops.
Only one  handler  is shared for each marke, in fact the last one from the
loop, the reason why you have the content of the last marker.
You have to create your handler from a 'function factory' like explained
here :
http://felix-kling.de/blog/2011/01/09/javascript-about-loops-functions-closures/

Regards
Yves


2013/2/28 dolphintroop <thymeland at hotmail.com>

> Hi,list:
> I just searched forum whole afternoon, and I still cannot find any clue
> about my question.
>
> Now I need to display multiple popups of markers on the map,and each popup
> has different content(such as the lat-lon of marker), and the content
> information need be loaded from background server directly(I considered the
> way load from txt,but it makes the thing complicated).
>
> below is the part of code, I suppose that the data has been loaded and put
> in an array of coodCollection [].
> //************************************************************
> var coodCollection
> =[10.926984840916,22.54309741608402,10.9270373628267,22.54429109286876];
>
>                 markers = new OpenLayers.Layer.Markers("Markers2");
>                 map.addLayer(markers);
>
>
>
>                 for ( var i = 0; i < (coodCollection.length / 2); i++) {
>
>                         var lon = coodCollection[i * 2];
>                         var lat = coodCollection[i * 2 + 1];
>
>                         var size = new OpenLayers.Size(21, 25);
>                         var offset = new OpenLayers.Pixel(-(size.w / 2),
> -size.h);
>                         var iconName = 'img/marker.png';
>
>
>
>
>                         var icon = new OpenLayers.Icon(iconName, size,
> offset);
>
>                         marker = new OpenLayers.Marker(new
> OpenLayers.LonLat(lon, lat),
>                                         icon.clone());
>
>                         marker.events.register("mouseover", marker,
> function(e) {
>                                 popup = new
> OpenLayers.Popup.FramedCloud("chicken",
>                                                 marker.lonlat, new
> OpenLayers.Size(200, 200),
>                                                 marker.lonlat, null, true);
>
>                                 map.addPopup(popup);
>                         });
>                         marker.events.register('mouseout', marker,
> function(evt) {
>                                 popup.hide();
>                         });
>
>                         markers.addMarker(marker);
>
>                 }
> //************************************************************
>
> Now the marker is displayed and  arranged as I wanted, the popup is jump
> out
> when I put mouse on marker, but all the content of popup is the last
> coordinate pair of coodCollection [], I don't know how to figure out the
> reason.
>
> Any suggestion is appreciated!
>
> Thank you in advanced!
>
>
>
>
>
>
> --
> View this message in context:
> http://osgeo-org.1560.n6.nabble.com/why-the-popups-have-same-content-tp5037498.html
> Sent from the OpenLayers Users mailing list archive at Nabble.com.
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20130228/c57d4c5f/attachment.html>


More information about the Users mailing list