Multiple Polygons unable to handle events

1,109 views
Skip to first unread message

d.bun

unread,
Nov 29, 2009, 7:26:34 AM11/29/09
to Google Maps JavaScript API v3
Hi there, I'm fairly new to building google maps.
I started a map that allows you to draw multiple markers, polylines,
and polygons.
In one instance I create multiple markers, I'm able to remove eachone
by adding an event listener with the following code:

marker = new google.maps.Marker({map: map, position: event.latLng});
google.maps.event.addListener(marker, 'click', remove);


However if I try to do the same with polygons, I can only remove the
most recently created polygon:

polygon = new google.maps.Polygon({paths: path, map: map});
google.maps.event.addListener(polygon, 'click', remove);

The same problem happens with polylines.

What's the difference between markers vs polylines & polygons? Would
someone know or can suggest a way to work around this or point me in
the right direction, where I'm able to create multiple polygons each
with their own events.

Ben Appleton

unread,
Nov 29, 2009, 6:49:17 PM11/29/09
to google-map...@googlegroups.com
Please provide a link to your site, and steps to reproduce the issue.  I have examples of multiple polygons with click events working fine, so I can't replicate this issue for myself.

Thanks
Ben


--

You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.



d.bun

unread,
Nov 29, 2009, 6:58:00 PM11/29/09
to Google Maps JavaScript API v3
I found one solution which was to dynamically generate variable names
with eval():

eval("polygon"+i+" = new google.maps.Polygon({paths: gonPath, map:
map})");
google.maps.event.addListener(eval("polygon"+i),'click',remove);
i++;

Is there perhaps another approach I'm overlooking?

Ben Appleton

unread,
Nov 29, 2009, 7:05:43 PM11/29/09
to google-map...@googlegroups.com
Yes.  It sounds like you've run into this classic JavaScript gotcha:

Please link your site for further comment.

Esa

unread,
Nov 29, 2009, 7:55:18 PM11/29/09
to Google Maps JavaScript API v3
I don't think we are that desperate that we have to take eval() in
action for this kind of basic feature.

Mike has a brilliant explanation in v2 enviroment. Find 'this doesn't
work'
http://econym.org.uk/gmap/basic1.htm

I made a similar marker function for v3 but I cannot clearly explain
the function closure
http://koti.mbnet.fi/ojalesa/boundsbox/makemarker.htm

d.bun

unread,
Nov 30, 2009, 1:07:02 AM11/30/09
to Google Maps JavaScript API v3
Thanks Esa and Ben, I was able to solve the problem fortunately
without resorting to using eval(). I don't know exactly what I did
because this:

polygon = new google.maps.Polygon({paths: path, map: map});
google.maps.event.addListener(polygon, 'click', remove);

the same bit of code that was causing me to scratch my chin was
working fine. I then found that:

google.maps.event.addListener(new google.maps.Polygon({paths: gonPath,
map: map}),'click',saveData);

would work better for my situation (which you know nothing about :))
here it is if your curious: http://dbun.pcriot.com/myturn.html

I have a new question that I'll pose about the "trigger" event
listener if I don't find anything about it.
Reply all
Reply to author
Forward
0 new messages