[OpenLayers-Users] Box Click Event

Steve Benzo steve_benzo at yahoo.com
Thu Nov 15 16:54:31 EST 2007


All,

I'm working with the boxes example included as part of the OpenLayers distribution, and am having some problems extending it. Basically, I want to create list of boxes, and allow users to click on those and be redirected to another location, specified in the "box_extents" array. I'm able to mostly do this by registering an event for the boxes, but herein lies the difficulty. The destination url for individual boxes (taken from box_extents) is always that of the last element in the box_extents array. As each box should direct to its array-given url, this is a big problem. Any insight or advice you how to make each box access its correct array parameters through that function would be greatly appreciated. THe code I'm using is below.


       var boxes  = new OpenLayers.Layer.Boxes( "Boxes" );

       var box_extents = [
            [-92.559, 42.889, -82.146, 43.126,'http://yahoo.com?','blue'],
            [-89.559, 40.889, -88.146, 41.126,'http://google.com?','blue2']
       ];

            var names = new Array("blue","blue2","red","red2","green","green2");

            for (var i = 0; i < box_extents.length; i++) {
                ext = box_extents[i];
                bounds = new OpenLayers.Bounds(ext[0], ext[1], ext[2], ext[3]);
                box = new OpenLayers.Marker.Box(bounds);
                box.setBorder("red",2);
                box.div.id = names[i];
                //alert(box.div.id);
                //this gets assigned correctly

                box.events.register("click", box, function (e) {
                    this.setBorder("yellow",2);
                    //alert(box.div.id);
                    //after click event, always the last element in the array (blue2)
                    //and subsequently, the wrong url for but the last array element
                    window.location.href=ext[4];
                    //wrong redirect :(
                });
                boxes.addMarker(box);
            }

map.addLayer(boxes);

       
---------------------------------
Get easy, one-click access to your favorites.  Make Yahoo! your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20071115/ac685105/attachment.html


More information about the Users mailing list