removeListener not working

476 views
Skip to first unread message

Andrew

unread,
Dec 16, 2011, 12:35:58 PM12/16/11
to google-map...@googlegroups.com
Hi I have a listener called MyCountyListener that when the page is loaded you can click on anywhere in Illinois and an infowindow will popup with a link to that county's webpage. This works fine.

I want it so that when I click on the "Measure Distance" button below my map, this listener goes away, and therefore I will be able to draw a polyline. Right now I am only able to draw a polyline outside of Illinois. 


I currently have 
google.maps.event.removeListener(MyCountyListener)
in my draw polyline function, but that is not working. 

Thank you.

Marcelo

unread,
Dec 16, 2011, 3:20:12 PM12/16/11
to Google Maps JavaScript API v3
You seem to be adding the listener multiple times when you call

for (var i = 0, c = fs.length; i < c; i++) {
setupFeature(fs[i]);
}

but then you remove it only once.

What you can do instead is to add the map click listener as soon as
the map is created and leave it active, and then inside that function
check a global boolean variable, for example:

var drawing = false; // global
...

// Inside the click listener
if (drawing == true) {
// do one thing
}
else {
// do another
}

--
Marcelo - http://maps.forum.nu
--

Andrew

unread,
Dec 19, 2011, 10:30:55 AM12/19/11
to google-map...@googlegroups.com
Thank you. This works well. In the else statement I changed it to i>c.

if (drawing==true){
var fs = rs.features;
  for (var i = 0, c = fs.length; i < c; i++) {
    setupFeature(fs[i]);
}
   }
else{
var fs = rs.features;

Michael Geary

unread,
Dec 19, 2011, 2:58:57 PM12/19/11
to google-map...@googlegroups.com
Assuming that rs.features is an array, this code doesn't make any sense at all. The loop in the else clause will run exactly zero times, because c (the array length) is greater than or equal to zero, and therefore i > c will be false the first time it's tested.

-Mike

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-maps-js-api-v3/-/WC175cV1GkYJ.

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.

Andrew

unread,
Dec 19, 2011, 3:25:06 PM12/19/11
to google-map...@googlegroups.com
Yes you're right. I forgot I had placed a statement in the initialize function as I was just testing things out. I'll work on it some more. Thank you!

Andrew

Andrew

unread,
Dec 21, 2011, 10:17:22 AM12/21/11
to google-map...@googlegroups.com
I actually changed the code somewhat by using this identify example. That way I only add a listener once instead of multiple times.

I did add the listener to the initialize function and then it was easy to remove the listener once i clicked on my measure tool button.
Reply all
Reply to author
Forward
0 new messages