[thelist] Googlemap script

Chris Price chris.price at choctaw.co.uk
Thu Mar 29 11:19:10 CDT 2012


Thanks Lee

That's excellent. Its elegant enough for me. It also makes more sense to me
now.

Chris.Price
@Choctaw.co.uk <http:\\choctaw.co.uk>
0777 629 0227

Design is my native language

Choctaw Media Ltd is a company registered in England and Wales (04627649)
On 29 March 2012 16:27, Lee Kowalkowski <lee.kowalkowski at googlemail.com>wrote:

> On 29 March 2012 13:24, Chris Price <chris.price at choctaw.co.uk> wrote:
>
> > I know the problem is that its picking up the last variable in a for next
>
> loop but I can't figure out the solution.
> >
>
> Yeah, declaring the marker inside your loop is misleading, because it will
> still be a function-scoped variable.  I mean, there is 1 marker variable,
> and when your loop is over, it is assigned to the last marker in your
> iteration.
>
> So when you do this:
>
>  google.maps.event.addListener(marker, 'click', function()
> {infowindow.open(map,marker); });
>
> marker is a closure over that function-scoped variable.
>
> The quickest/easiest solution to this is to create another function to
> obtain a separate closure for each marker:
>
> function addMarkerClickEventHandler(infowindow, map, marker)
> {
>  google.maps.event.addListener(marker, 'click', function()
> {infowindow.open(map,marker); });
> }
>
> and call addMarkerClickEventHandler(infowindow, map, marker) inside your
> loop.
>
> There are possibly more elegant ways to solve it, e.g. by making your event
> handler reference 'this' instead of 'marker', but that depends on the
> execution context in which the event handler is going to be called (I don't
> know if there's anything special about the google maps API that would
> prevent this from working as expected).  And then there's also assigning
> the click event handler to a parent element instead of each individual
> marker, and relying on event bubbling, then inspecting the event to figure
> out which marker had been clicked.  More elegant, but also more involved!
>
> --
> Lee
> www.webdeavour.co.uk
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>


More information about the thelist mailing list